STL : find ()



#include <algorithm> // for find ()
#include <vector>
using namespace std;

int main ()
{
    int x [] = {2, 4, 6, 8, 10};

    vector <int> v ( &x [0], &x [5] );
    // vector represents array 'x'

    int key = 6;
    vector <int> :: iterator p;

    p = find ( v.begin (), v.end (), key );

    if ( *p == key )
    printf ("key : %d found\n", *p);

    return 0;
}

/* output
key : 6 found

Process returned 0 (0x0)   
execution time : 0.047 s
Press any key to continue.
*/


If the value of key is equals to: 2 / 4 / 6 / 8 / 10 output will be found.

vector <int>:: iterator p;
declares a variable p, that traverse the whole vector form begin to end.

p saves the address of key, if found. So,
If (*p is equals to key) then, output: found

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s