C ++: std :: vector :: reserve is not reserved when specifying pointers

When I call std::vector::reserve, when the identifier is of type std::vector<Foo*> reserve(...)does nothing:

std::vector<int*> bar;
bar.reserve(20);

//I expect bar.size to return 20...
std::size_t sz = bar.size();
for(std::size_t i = 0; i < sz; ++i) {
    //Do Stuff to all items!
}

The above loop forruns exactly at time zero, and bar.size () returns zero. I don’t remember if this is true for all other STL containers, but if so, including the behavior for std :: vector: WHY?

+5
source share
3 answers

.reserve() . -, , -.resize(). reserve() - . push_back(), , reserve(), . reserve() .

+12
+7

vector::reserve()changes the capacity of the vector, not the size .

capacity- how much memory was allocated inside to hold the elements of the vector. size- how many elements the vector actually holds. vector::resize()affects the latter.

+6
source

All Articles