C++ STL map, value_comp() program sample

 

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

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

 

// C++ STL map, value_compare

#include <map>

#include <iostream>

using namespace std;

 

int main(void)

{

// map container

map <int, int, less<int> > m1;

 

map <int, int, less<int> >::value_compare vc1 = m1.value_comp();

pair<map<int, int>::iterator, bool> pr1, pr2;

pr1= m1.insert(map <int, int> :: value_type (1, 10));

pr2= m1.insert(map <int, int> :: value_type (2, 5));

 

cout<<"\nOperation: vc1(*pr1.first, *pr2.first) == true?\n";

if(vc1(*pr1.first, *pr2.first) == true)

{

cout<<"The element (1,10) precedes the element (2,5)."<<endl;

}

else

{

cout<<"The element (1,10) does not precede the element (2,5)."<<endl;

}

cout<<"\nOperation: vc1(*pr2.first, *pr1.first) == true?\n";

if(vc1(*pr2.first, *pr1.first) == true)

{

cout<<"The element (2,5) precedes the element (1,10)."<<endl;

}

else

{

cout << "The element (2,5) does not precede the element (1,10)."<< endl;

}

return 0;

}

 

Output examples:

 

Operation: vc1(*pr1.first, *pr2.first) == true?

The element (1,10) precedes the element (2,5).

Operation: vc1(*pr2.first, *pr1.first) == true?

The element (2,5) does not precede the element (1,10).

Press any key to continue . . .

 

 

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