what is an iterator loop c++ -vector code example
Example 1: how to iterate trough a vector in c++
vector<int> myVector;
myVector.push_back(1);
myVector.push_back(2);
myVector.push_back(3);
myVector.push_back(4);
for(auto x: myVector){
cout<< x << " ";
}
vector<pair<int,int>> myVectorOfPairs;
myVectorOfPairs.push_back({1,2});
myVectorOfPairs.push_back({3,4});
myVectorOfPairs.push_back({5,6});
myVectorOfPairs.push_back({7,8});
for(auto x: myVectorOfPairs){
cout<< x.first << " " << x.second << endl;
}
Example 2: c++ vector iterator
vector<string> vData;
vData.push_back("zeroth");
vData.push_back("first");
vData.push_back("second");
vData.push_back("third");
std::vector<string>::iterator itData;
for (itData = vData.begin(); itData != vData.end() ; itData++)
{
auto ElementIndex = itData-vData.begin();
auto ElementValue = vData[ElementIndex];
cout << "[ElementIndex:" << ElementIndex << "][ElementValue:" << ElementValue << "]\n";
}