Does the STL vector resize / invalidate its previous contents?

It does not appear (example program), but can I be sure?

// does resizing an STL vector erase/invalidate it previous contents? #include <stdio.h> #include <vector> using namespace std ; void print( vector<int>& t ) { for( int i = 0 ; i < t.size() ; i++ ) printf( "%d ", t[i] ) ; puts(""); } int main() { vector<int> t ; t.resize( 12,9999 ) ; print(t) ; t.resize( 15, 10000 ) ; print(t) ; } 
+6
source share
6 answers

Resizing an STL vector may require reallocation of the underlying storage. This can lead to the destruction and recreation of any number of elements and all iterators invalid. Access to an invalid iterator is a common source of errors when using STL. p>

the contents of each element will be the same if the copy constructor does not work.

 int main(int argc, char *argv[]) { int data[] = { 1, 2, 3 }; std::vector vec(data, data + 3); // vector contains 1, 2, 3 std::vector::iterator i = vec.begin(); cout << *i << endl; // prints 1 int &ref = *i; cout << ref << endl; // prints 1 vec.resize(6, 99); // vector now contains 1, 2, 3, 99, 99, 99 // WRONG! may crash, may do the wrong thing, might work... // cout << *i << endl; // WRONG! invalid reference // cout << ref << endl; return 0; } 
+16
source

resize will invalidate all iterators, pointers, and references in std::vector if and only if the new size is larger than the current container capacity (i.e. v.capacity() ).

Items in the container are never "invalid". If you resize the container smaller than its current size, all additional elements outside the new size will be destroyed.

If you resize the container so that the new size is larger than the current capacity and you need to reallocate the base storage, all items will be copied or moved to the newly allocated storage. When you enlarge the container, the previous items are always saved, simply moved or copied to new places in memory.

+6
source

Resizing a vector does not destroy the values ​​stored in the vector (with the exception of those that exceed the new size during compression, of course), however, a growing vector, beyond its capabilities, copies (or, in C ++ 11, moves) them to a new location, such invalid, and iterators, pointers, or references to these elements.

In your sample program, you do not store iterators, pointers, or references to vector elements during resizing, so you get access to copied values ​​if the data was copied during resizing (which is probably, but not completely accurate, the vector can allocate space for more than necessary elements, because with its growth it is often necessary to fulfill the complexity requirements).

You can get the current capacity (the number of elements that you can grow before redistributing) through the capacity member function. As long as the vector does not go beyond the current capacity, even iterators, pointers, and references to stored objects are safe. In addition, if you want to make sure that iterators, pointers, or references are invalid, and you know in advance the maximum size by which the vector can grow, you can preallocate all the necessary memory using the reserve member function.

+2
source

Resizing copies data as much as it fits. Take a look at this http://www.cplusplus.com/reference/stl/vector/resize/

0
source

Content will still be. If your vector has a capacity of 1000, with 500 elements in it, and you resize to 2000, all of your 500 elements will be copied. If you resize to 200, only the first 200 items will be copied, and the last 300 will be deleted.

0
source

Here is an example here .

When you resize to a larger size, it just fills the new cells with value.

0
source

Source: https://habr.com/ru/post/910113/


All Articles