C++ STL hash_map, operator>= 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++ operator>= to test if the hash_map object on the left side of the operator is greater than or equal to the hash_map object on the right side in C++ programming

To show: How to use the C++ hash_map, operator>= to test if the hash_map object on the left side of the operator is greater than or equal to the hash_map object on the right side in C++ programming

 

// C++ STL hash_map, operator>=

// In Visual C++ .NET 2003, members of the <hash_map> and <hash_set> header files are

// no longer in the std namespace, but rather have been moved into the stdext namespace.

#include <hash_map>

#include <iostream>

using namespace std;

using namespace stdext;

 

int main(void)

{

int i;

// hash_map containers

hash_map <int, int> hm1, hm2, hm3, hm4;

// hash_map iterators

hash_map <int, int>::iterator hm1_Iter, hm2_Iter, hm3_Iter, hm4_Iter;

typedef pair <int, int> Int_Pair;

 

// inserting data

for (i = 1; i < 3; i++)

{

hm1.insert(Int_Pair(i, i));

hm2.insert(Int_Pair(i, i*i));

hm3.insert(Int_Pair(i, i-1));

hm4.insert(Int_Pair(i, i));

}

// some operations

cout<<"Operation: hash_map <int, int> hm1\n";

cout<<"hm1 hash_map data: ";

for(hm1_Iter = hm1.begin(); hm1_Iter != hm1.end(); hm1_Iter++)

cout<<hm1_Iter->second<<" ";

cout<<endl;

cout<<"Operation: hash_map <int, int> hm2\n";

cout<<"hm2 hash_map data: ";

for(hm2_Iter = hm2.begin(); hm2_Iter != hm2.end(); hm2_Iter++)

cout<<hm2_Iter->second<<" ";

cout<<endl;

cout<<"Operation: hash_map <int, int> hm3\n";

cout<<"hm3 hash_map data: ";

for(hm3_Iter = hm3.begin(); hm3_Iter != hm3.end(); hm3_Iter++)

cout<<hm3_Iter->second<<" ";

cout<<endl;

cout<<"Operation: hash_map <int, int> hm4\n";

cout<<"hm4 hash_map data: ";

for(hm4_Iter = hm4.begin(); hm4_Iter != hm4.end(); hm4_Iter++)

cout<<hm4_Iter->second<<" ";

cout<<endl;

// more operations

if (hm1 >= hm2)

cout<<endl<<"The hash_map hm1 is greater than or equal to the hash_map hm2."<<endl;

else

cout<<endl<<"The hash_map hm1 is less than the hash_map hm2."<<endl;

if (hm1 >= hm3)

cout<<"The hash_map hm1 is greater than or equal to the hash_map hm3."<<endl;

else

cout<<"The hash_map hm1 is less than the hash_map hm3."<<endl;

if (hm1 >= hm4)

cout<<"The hash_map hm1 is greater than or equal to the hash_map hm4."<<endl;

else

cout<<"The hash_map hm1 is less than the hash_map hm4."<<endl;

return 0;

}

 

Output examples:

 

Operation: hash_map <int, int> hm1

hm1 hash_map data: 1 2

Operation: hash_map <int, int> hm2

hm2 hash_map data: 1 4

Operation: hash_map <int, int> hm3

hm3 hash_map data: 0 1

Operation: hash_map <int, int> hm4

hm4 hash_map data: 1 2

The hash_map hm1 is less than the hash_map hm2.

The hash_map hm1 is greater than or equal to the hash_map hm3.

The hash_map hm1 is greater than or equal to the hash_map hm4.

Press any key to continue . . .

 

 

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