C++ STL algorithm, lexicographical_compare() 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++ lexicographical_compare() to compare element by element between two sequences to determine which is lesser of the two in C++ programming

To show: How to use the C++ algorithm, lexicographical_compare() to compare element by element between two sequences to determine which is lesser of the two in C++ programming

 

// C++ STL algorithm, lexicographical_compare()

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

 

bool result1, result2, result3;

 

// pushing/inserting data

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

vec1.push_back(5*i);

for(j = 0; j <= 6; j++)

lst.push_back(5*j);

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

vec2.push_back(10*k);

 

// printing 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;

// self lexicographical_comparison() of vec1 under identity

cout<<"\nOperation: lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end())"<<endl;

result1 = lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end());

if(result1)

cout<<"vec1 vector is lexicographically_less than vec2 vector"<<endl;

else

cout<<"vec1 vector is not lexicographically_less than vec2 vector"<<endl;

// lexicographical_comparison() of vec1 and lst under identity

cout<<"\nOperation: lexicographical_compare(vec1.begin(), vec1.end(), lst.begin(), lst.end())"<<endl;

result2 = lexicographical_compare(vec1.begin(), vec1.end(), lst.begin(), lst.end());

if(result2)

cout<<"vec1 vector is lexicographically_less than lst list"<<endl;

else

cout<<"vec1 vector is lexicographically_less than lst list"<<endl;

cout<<"\nOperation: lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice)"<<endl;

result3 = lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice);

if(result3)

cout<<"vec1 vector is lexicographically_less than vec2 vector based on twice()"<<endl;

else

cout<<"vec1 vector is not lexicographically_less than vec2 vector based on twice()"<<endl;

return 0;

}

 

Output examples:

 

vec1 vector data: 0 5 10 15 20 25

lst list data: 0 5 10 15 20 25 30

vec2 vector data: 0 10 20 30 40 50

Operation: lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end())

vec1 vector is lexicographically_less than vec2 vector

Operation: lexicographical_compare(vec1.begin(), vec1.end(), lst.begin(), lst.end())

vec1 vector is lexicographically_less than lst list

Operation: lexicographical_compare(vec1.begin(), vec1.end(), vec2.begin(), vec2.end(), twice)

vec1 vector is not lexicographically_less than vec2 vector based on twice()

Press any key to continue . . .

 

 

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