|
|
|
vector<T> Class - find() Function Example
% cat vect_find.cpp
// vector<T> example - find() function
#include <iostream>
using namespace std;
#include <vector>
#include<algorithm>
int main()
{
vector<int> intVec;
intVec.push_back(98);
intVec.push_back(72);
intVec.push_back(84);
intVec.push_back(73);
intVec.push_back(91);
cout << "Display using iterator notation:\n";
vector<int>::iterator vecIter = intVec.begin();
while(vecIter != intVec.end() )
{
cout << *vecIter << " ";
vecIter++;
}
cout << endl;
// find
int key;
cout << "Enter a value to find: ";
cin >> key;
vector<int>::iterator location;
location = find( intVec.begin(), intVec.end(), key);
if( location != intVec.end() )
cout << *location << " value
found" << endl;
else
cout << "value not found" <<
endl;
return 0;
}
% g++ vect_find.cpp
% a.out
Display using iterator notation:
98 72 84 73 91
Enter a value to find: 84
84 value found
% a.out
Display using iterator notation:
98 72 84 73 91
Enter a value to find: 20
value not found
|
|