C++ STL algorithm, find_first_of() code example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional project setting: Set project to be compiled as C++

Project -> your_project_name Properties -> Configuration Properties -> C/C++ -> Advanced -> Compiled As: Compiled as C++ Code (/TP)

Other info: none

To do: Using the C++ find_first_of() to search for the first occurrence of any of several values within a target range or for the first occurrence of any of several elements that are equivalent in a sense specified by a binary predicate to a specified set of the elements in C++ programming

To show: How to use the C++ algorithm, find_first_of() to search for the first occurrence of any of several values within a target range or for the first occurrence of any of several elements that are equivalent in a sense specified by a binary predicate to a specified set of the elements in C++ programming

 

 

 

// C++ STL algorithm, find_first_of()

#include <vector>

#include <list>

#include <algorithm>

#include <iostream>

using namespace std;

 

// return whether second element is twice the first

bool twice(int elem1, int elem2)

{return ((2 * elem1) == elem2);}

 

int main(void)

{

// vector containers

vector <int> vec1, vec2;

// list container

list <int> lst;

// vector iterators

vector <int>::iterator Iter1, Iter2;

// list iterators

list <int>::iterator lst_Iter, lst_inIter;

int i, j, k;

 

// pushing data, construction vector and list

for(i = 0; i <= 5; i++)

vec1.push_back(5*i);

for(j = 3; j <= 4; j++)

lst.push_back(5*j);

for(k = 2; k <= 4; k++)

vec2.push_back(10*k);

 

// printing the data

cout<<"vec1 vector data: ";

for(Iter1 = vec1.begin(); Iter1 != vec1.end(); Iter1++)

cout<<*Iter1<<" ";

cout<<endl;

cout<<"lst list data: ";

for(lst_Iter = lst.begin(); lst_Iter!= lst.end(); lst_Iter++)

cout<<*lst_Iter<<" ";

cout<<endl;

cout<<"vec2 vector data: ";

for(Iter2 = vec2.begin(); Iter2 != vec2.end(); Iter2++)

cout<<*Iter2<<" ";

cout<<endl;

// search, find_first_of() operation, searching vec1 for first match to lst under identity

vector <int>::iterator result1;

result1 = find_first_of(vec1.begin(), vec1.end(), lst.begin(), lst.end());

if(result1 == vec1.end())

cout<<"\nThere is no match of lst list in vec1 vector"<<endl;

else

cout<<"\nThere is at least one match of lst list in vec1 vector and the first one begins at "

<<"position "<<result1 - vec1.begin()<<endl;

 

// another search using find_first_of(), searching vec1 vector for a match to lst list under the binary predicate twice()

vector <int>::iterator result2;

result2 = find_first_of(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice);

if(result2 == vec1.end())

cout<<"\nThere is no match of lst list in vec1 vector"<<endl;

else

cout<<"\nThere is a sequence of elements in vec1 vector that are equivalent to those in vec2 vector\nunder the binary"

<<" predicate twice() and the first one begins at position "<<result2 - vec1.begin()<<endl;

return 0;

}

 

Output examples:

 

vec1 vector data: 0 5 10 15 20 25

lst list data: 15 20

vec2 vector data: 20 30 40

There is at least one match of lst list in vec1 vector and the first one begins at position 3

There is a sequence of elements in vec1 vector that are equivalent to those in vec2 vector

under the binary predicate twice() and the first one begins at position 2

Press any key to continue . . .

 

 

C and C++ Programming Resources | C & C++ Code Example Index