C++ STL algorithm, find_end() program sample

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

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_end() to look in a range for the last subsequence that is identical to a specified sequence or that is equivalent in a sense specified by a binary predicate in C++ programming

To show: How to use the C++ algorithm, find_end() to look in a range for the last subsequence that is identical to a specified sequence or that is equivalent in a sense specified by a binary predicate in C++ programming

// C++ STL algorithm, find_end()

#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, constructing the vectors and list

for(i = 10; i <= 15; i++)

vec1.push_back(i);

for(j = 11; j <= 14; j++)

lst.push_back(j);

for(k = 12; k <= 14; k++)

vec2.push_back(2*k);

// print 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<<endl;

// searching vec1 for a match to lst under identity, do the find_end() operation

vector <int>::iterator result1;

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

// print the result

if(result1 == vec1.end())

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

else

cout<<"There is a match of lst list in vec1 vector that begins at position "<<result1 - vec1.begin()<<endl;

// searching vec1 for a match to lst under the binary predicate twice(), do another find_end() operation

vector <int>::iterator result2;

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

// print the result

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\n vec2 vector under the binary "

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

return 0;

}

Output examples:

vec1 vector data: 10 11 12 13 14 15

lst list data: 11 12 13 14

vec2 vector data: 24 26 28

There is a match of lst list in vec1 vector that begins at position 1

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

vec2 vector under the binary predicate twice() and that begins at position 2

Press any key to continue . . .