C++ vector loop via iterator
#include <iostream> #include <array> #include <vector> #include <ios> using namespace std; int main(int argc, char *argv []) { /* www . j a va 2s . c o m*/ using MyVector = vector<int>; for (unsigned int i = 0; i < myVector.size(); ++i) { if (myVector[i] == 2) { cout << "Found: " << myVector[i] << " at position: " << i << endl; } } for (auto iter = myVector.begin(); iter != myVector.end(); ++iter) { if (*iter == 2) { cout << "Found: " << *iter << endl; } } for (auto& number : myVector) { if (number == 2) { cout << "Found : " << number << endl; } } MyVector::iterator found = find(myVector.begin(), myVector.end(), 2); if (found != myVector.end()) { cout << "Found : " << *found << endl; } return 0; }