#include <iostream> // std::cout
#include <iterator> // std::distance
#include <vector> // std::vector
int main () {
std::vector<int> myvector;
// for (int i=0; i<10; i++) myvector.push_back (i*10);
myvector.reserve(100);
std::vector<int>::iterator first = myvector.begin();
std::vector<int>::iterator last = myvector.end();
std::cout << "The distance is: " << std::distance(first,last) << '\n'; // 0
/// I want to do this == myvector.resize(100);
/// THIS LINE DOESNOT WORK
(*first).resize(100);
return 0;
}
In case the container shrinks, all iterators, pointers and references to elements that have not been removed remain valid after the resize and refer to the same elements they were referring to before the call.
If the container expands, the end iterator is invalidated and, if it has to reallocate storage, all iterators, pointers and references related to this container are also invalidated.
With questions like these... it's really more helpful to ask what you're actually trying to do accomplish.
For example, you can pass a vector by reference if you want another part of the code to be able to resize it. (Again, note that resizing a vector can invalidate iterators)
*first isnt a vector, its an int. ^^^ explicitly stating what he said in first line there.
also consider
vector<int> invec[100];
invec[97] = 99; //ok
it looks like that is what you want (a fixed size?). Its not hard-fixed (pushback etc will grow it) but if you don't do anything to make it change, it will behave mostly like int invec[100] would.