C++ STL set, value_comp() program example

 

Compiler: Visual C++ Express Edition 2005

Compiled on Platform: Windows XP Pro SP2

Header file: Standard

Additional library: none/default

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++ value_comp() to retrieve a copy of the comparison object used to order element values in a set in C++ programming

To show: How to use the C++ set, value_comp() to retrieve a copy of the comparison object used to order element values in a set in C++ programming

 

// C++ STL set, value_comp()

#include <set>

#include <iostream>

using namespace std;

 

int main(void)

{

// containers

set <int, less<int> > st1;

set <int, less<int> >::value_compare vcom1 = st1.value_comp();

 

bool result1 = vcom1(5, 9);

// do the testing

if(result1 == true)

{

cout<<"vcom1(5,9) returns value of true, where vcom1 is the function object of st1 set"<<endl;

}

else

{

cout<<"vcom1(5,9) returns value of false, where vcom1 is the function object of st1 set"<<endl;

}

// containers

set <int, greater<int> > st2;

set<int, greater<int> >::value_compare vcom2 = st2.value_comp();

bool result2 = vcom2(5, 9);

if(result2 == true)

{

cout<<"vcom2(5,9) returns value of true, where vcom2 is the function object of st2 set"<<endl;

}

else

{

cout<<"vcom2(5,9) returns value of false, where vcom2 is the function object of st2 set"<<endl;

}

return 0;

}

 

Output examples:

 

vcom1(5,9) returns value of true, where vcom1 is the function object of st1 set

vcom2(5,9) returns value of false, where vcom2 is the function object of st2 set

Press any key to continue . . .

 

 

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