C++ STL set, key_comp() code 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++ key_comp() to retrieve a copy of the comparison object used to order keys in a set in C++ programming

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

 

// C++ STL set, key_comp()

#include <set>

#include <iostream>

using namespace std;

 

int main(void)

{

// container

set <int, less<int> > st1;

set<int, less<int> >::key_compare kc1 = st1.key_comp();

 

bool res1 = kc1(3, 7);

if(res1 == true)

{

cout<<"kc1(3,7) returns value of true, where kc1 is the function object of st1 set"<<endl;

}

else

{

cout<<"kc1(3,7) returns value of false where kc1 is the function object of st1 set"<<endl;

}

set <int, greater<int> > st2;

set<int, greater<int> >::key_compare kc2 = st2.key_comp();

bool res2 = kc2(3, 7);

if(res2 == true)

{

cout<<"kc2(3,7) returns value of true, where kc2 is the function object of st2 set"<<endl;

}

else

{

cout<<"kc2(3,7) returns value of false, where kc2 is the function object of st2 set"<<endl;

}

return 0;

}

 

Output examples:

 

kc1(3,7) returns value of true, where kc1 is the function object of st1 set

kc2(3,7) returns value of false, where kc2 is the function object of st2 set

Press any key to continue . . .

 

 

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