I made a vector and i wanted the user to enter names and then stop to stop, but after i do that the program outputs what i entered then crashes. It says the program has stopped working.
iterator is a specialized variable used to move through a container, in this case a vector. in c++11 they came out with what they call a range-based for loop, which in other languages is typically called a foreach loop. im not sure if i did the condition correct (i havent needed to use it before) but if it doesnt im sure someone will correct me. but what it does is automatically iterate through the vector for you
The problem with the original code is that vectSize >= 1 will not change so if it is true when you reach the loop it will stay true. That means there is nothing preventing i from growing bigger than there are elements in the vector, which will eventually cause a crash. Using a loop condition i < vectSize is probably what you want.
If you want to use iterators, like DTSCode tried to show it will look something like this (without range-based for loop):
1 2 3 4
for(vector<string>::iterator it = strVect_one.begin(); it != strVect_one.end(); ++it)
{
cout << "Names: " << *it << endl;
}
And using a range-based for loop it could look something like this: