1: //find1a.cpp 3: #include <iostream> 4: #include <vector> 5: #include <algorithm> 6: using namespace std; 8: int main() 9: { 10: cout << "\nThis program illustrates the use of the STL find() " 11: "algorithm\nto find a given integer value within a vector " 12: "of integers."; 13: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 15: int a[] = {2, 1, 4, 3, 6, 5, 8, 7, 4, 2, 10, 9}; 16: vector<int> v(a, a+12); 17: cout << "\nHere are the contents of v:\n"; 18: for (vector<int>::size_type i=0; i<v.size(); i++) 19: cout << v.at(i) << " "; 20: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 22: vector<int>::iterator p; 24: p = find(v.begin(), v.end(), 4); 25: if (p != v.end()) 26: cout << "\nThe first instance of 4 occurs at location " 27: << (int)(p-v.begin()+1) << "."; 28: else 29: cout << "\nThe value 4 was not found."; 30: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 32: p = find(p+1, v.end(), 4); 33: if (p != v.end()) 34: cout << "\nThe next instance of 4 occurs at location " 35: << (int)(p-v.begin()+1) << "."; 36: else 37: cout << "\nThe value 4 was not found among the remaining values."; 38: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 40: p = find(p+1, v.end(), 4); 41: if (p != v.end()) 42: cout << "\nThe next instance of 4 occurs at location " 43: << (int)(p-v.begin()+1) << "."; 44: else 45: cout << "\nThe value 4 was not found among the remaining values."; 46: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 48: p = find(v.begin(), v.end(), 7); 49: if (p != v.end()) 50: cout << "\nThe first instance of 7 occurs at location " 51: << (int)(p-v.begin()+1) << "."; 52: else 53: cout << "\nThe value 7 was not found."; 54: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 56: p = find(v.begin(), v.end(), 17); 57: if (p != v.end()) 58: cout << "\nThe first instance of 17 occurs at location " 59: << (int)(p-v.begin()+1) << "."; 60: else 61: cout << "\nThe value 17 was not found."; 62: cout << "\nPress Enter to continue ... "; cin.ignore(80, '\n'); 63: }