C++ STL algorithm, search() program 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++ search() to search for the first occurrence of a sequence within a target range whose elements are equal to those in a given sequence of elements or whose elements are equivalent in a sense specified by a binary predicate to the elements in the given sequence in C++ programming

To show: How to use the C++ algorithm, search() to search for the first occurrence of a sequence within a target range whose elements are equal to those in a given sequence of elements or whose elements are equivalent in a sense specified by a binary predicate to the elements in the given sequence in C++ programming

 

// C++ STL algorithm, search()

 

#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)

{

// containers

vector <int> v1, v2;

list <int> lst1;

// iterators

vector <int>::iterator Iter1, Iter2;

list <int>::iterator lst1_Iter;

int i, j, k;

 

// pushing data in ranges

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

{

v1.push_back(5 * i);

}

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

{

v1.push_back(5 * i);

}

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

{

lst1.push_back(5 * j);

}

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

{

v2.push_back(10 * k);

}

 

// print the data

cout<<"v1 vector: ";

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

cout<<*Iter1<<" ";

cout<<endl;

cout<<"\nlst1 list: ";

for (lst1_Iter = lst1.begin(); lst1_Iter!= lst1.end(); lst1_Iter++)

cout<<*lst1_Iter<<" ";

cout<<endl;

cout<<"\nv2 vector: ";

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

cout<<*Iter2<<" ";

cout<<endl;

// searching v1 for first match to lst1 under identity

vector <int>::iterator result1;

result1 = search(v1.begin(), v1.end(), lst1.begin(), lst1.end());

if (result1 == v1.end())

cout<<"\nThere is no match of lst1 in v1."<<endl;

else

cout<<"\nThere is at least one match of lst1 in v1 and the first one begins at position "<<int(result1 - v1.begin())<<endl;

// searching v1 for a match to lst1 under the binary predicate twice

vector <int>::iterator result2;

result2 = search (v1.begin(), v1.end(), v2.begin(), v2.end(), twice);

if (result2 == v1.end())

cout<<"\nThere is no match of v2 in v1."<<endl;

else

cout<<"\nThere is a sequence of elements in v1 that are equivalent\n to those in v2 under the binary "

<<"predicate twice()\n and the first one begins at position "<<int(result2 - v1.begin())<<endl;

return 0;

}

 

Output examples:

 

v1 vector: 0 5 10 15 20 25 0 5 10 15 20 25

lst1 list: 20 25

v2 vector: 20 30 40

There is at least one match of lst1 in v1 and the first one begins at position 4

There is a sequence of elements in v1 that are equivalent

to those in v2 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