Linked Questions
34 questions linked to/from std::vector::resize() vs. std::vector::reserve()
4 votes
3 answers
18k views
C++ Vector size is returning zero [duplicate]
The size() for vectors in C++ standard library returns zero The size() is supposed to return the current number of elements. Is something wrong with my code ? Or is it a bug in the function ? It ...
-3 votes
2 answers
7k views
What does vector::reserve do? [duplicate]
What does vector::reserve actually do since trying to access its data is an error? vector<int> v.reserve(10); v[4] = 22; if this allocated space it shouldn't be an error in the first place.. ...
2 votes
3 answers
2k views
Weirdness of the reserve() of vector [duplicate]
I wrote the following code to test the value of capacity of a vector when it was constructed from another. This was inspired by a question asked here. #include <iostream> #include<vector> ...
2 votes
1 answer
3k views
std::vector size does not update after reserve and fill [duplicate]
I need to fill a vector in with particular values. I find that the below code works, except in that a.size() fails to change from 0. Adding a resize call after I put in the elements takes almost twice ...
1 vote
2 answers
1k views
Creating a vector of char pointers in C++ [duplicate]
I have to make a vector that stores char pointers. Each pointer should only point to a single char like 'a' or 'b'. I don't get any compile errors, but when I run my program, it crashes. I cannot use ...
3 votes
2 answers
476 views
std::vector<std::vector<int>>: Debug assertion failed. C++ vector subscript out of range reserving memory [duplicate]
What's wrong here? std::vector<std::vector<int>> mSectionsSubsets; int count = (int)powf(2, NUM_SECTIONS); mSectionsSubsets.reserve(count); for (int i = 0; i < count; i++) { ...
0 votes
1 answer
1k views
how to reserve vector space for class objects? [duplicate]
If I have a class's header file like the following: class arrayStack { private: struct StackNode { StackNode(int p, int v):previous(p),value(v){} ~StackNode(){std::cout<<...
0 votes
1 answer
293 views
Operator= doesn't work with std::vector c++ [duplicate]
I have a class template <class T> class General_matrix : public Math_object<T> { public: General_matrix(const size_t m, const size_t n); ~General_matrix(); ...
0 votes
1 answer
210 views
How to use std::copy? not working on OSXSierra/CLION/C++11. [duplicate]
I was unable to make std::copy works. I tried several codes in internet, but I was unable to make it work. I need to use copy, first, to understand why is not working, second, to use it in a ...
1 vote
1 answer
227 views
string::assign doesn't work in vector of strings [duplicate]
Why this program crashes? It crashes on line vec[0].assign("blabla");: #include <vector> #include <string> using namespace std; int main() { vector<string> vec; vec....
2 votes
1 answer
87 views
What's the difference between the following ways of adding elements to a c++ vector [duplicate]
Snippet1: The following snippet prints out 0 1 but returns an empty vector. vector<int> trial() { vector<int> ret; ret.reserve(2); ret[0] = 0; ret[1] = 1; cout <&...
1 vote
2 answers
78 views
C++ vectors. Insertion with [] [duplicate]
I initialized a vector in my c++ program, then reserved some memory to store values in it. Why I can't assign value to the memory block I had reserved earlier? vector<int> test; test.reserve(100)...
-1 votes
2 answers
132 views
difference between `explicit vector( size_type count, const Allocator& alloc = Allocator() );` and `std::vector<T,Allocator>::reserve` [duplicate]
What's the difference between std::vector<uint8_t> vec1(size); and std::vector<uint8_t> vec2; vec2.reserve(size); Do they both allocate memory on heap?
0 votes
1 answer
119 views
How do these three ways of creating vectors differ? [duplicate]
While working on a submission, I found behaviour that I don't understand. I have three ways of populating my matrix from the input. One of them works, one of them compiles and runs but produces a ...
0 votes
1 answer
133 views
reserving memory for 2D vector cause segmentation fault in swig [duplicate]
Consider the simple code: I reserve memory for 2d vector, import the module in python, make instance of class and run the python code once. The second run of the python code make an error: #include &...