Search value in vector - C++ STL Algorithm

C++ examples for STL Algorithm:find

Description

Search value in vector

Demo Code

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>

using namespace std;

int main()//w w w. java2s .  com
{
    vector<string> Words;

    Words.push_back("Blue");
    Words.push_back("Green");
    Words.push_back("Teal");
    Words.push_back("Brick");
    Words.push_back("Purple");
    Words.push_back("Brown");
    Words.push_back("LightGray");

    vector<string>::iterator Result = find(Words.begin(), Words.end(), "LightGray");

    if (Result != Words.end())
        cout << *Result << endl;
    else
        cout << "Value not found!" << endl;

    Result = find(Words.begin(), Words.end(), "Black");

    if (Result != Words.end())
        cout << *Result << endl;
    else
        cout << "Value not found!" << endl;
}

Result


Related Tutorials