C++ examples for STL Algorithm:find
Finding Values in a vector
#include <iostream> #include <array> #include <vector> #include <ios> using namespace std; int main(int argc, char *argv[]) { using MyVector = vector<int>; MyVector myVector = { 0, 1, 2 };// ww w .java 2s . co m 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; }