C++ STL multimap, key_compare 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:

To do: Using the C++ key_compare which is a type that provides a function object that can compare two sort keys to determine the relative order of two elements in the multimap in C++ programming

To show: How to use the C++ multimap, key_compare key_compare which is a type that provides a function object that can compare two sort keys to determine the relative order of two elements in the multimap in C++ programming

 

// C++ STL multimap, key_compare

#include <map>

#include <iostream>

using namespace std;

 

int main(void)

{

multimap <int, int, less<int> > m1;

multimap <int, int, less<int> >::key_compare kcomp1 = m1.key_comp();

 

bool result1 = kcomp1(2, 3);

if(result1 == true)

{

cout<<"kcomp1(2,3) returns value of true, where kcomp1 is the function object of m1."<<endl;

}

else

{

cout<<"kcomp1(2,3) returns value of false where kcomp1 is the function object of m1."<<endl;

}

 

multimap <int, int, greater<int> > m2;

multimap <int, int, greater<int> >::key_compare kcomp2 = m2.key_comp();

bool result2 = kcomp2(2, 3);

 

if(result2 == true)

{

cout<<"kcomp2(2,3) returns value of true, where kcomp2 is the function object of m2."<<endl;

}

else

{

cout<<"kcomp2(2,3) returns value of false, where kcomp2 is the function object of m2."<<endl;

}

return 0;

}

 

Output examples:

 

kcomp1(2,3) returns value of true, where kcomp1 is the function object of m1.

kcomp2(2,3) returns value of false, where kcomp2 is the function object of m2.

Press any key to continue . . .

 

 

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