Gold University of Minnesota M. Skip to main content.University of Minnesota. Home page.
 
 
 

What's inside.

Ta Email

Download Compiler

Final Project

Lab Notes

Office Hours

Schedule

Syllabus

Announcements

Check Grades

 

CSci 1113 Home

 
 

Printer-friendly version

 
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
 
The University of Minnesota is an equal opportunity educator and employer.
CSci 1113: C++ Programming