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 not 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 not 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)

{

// hash_map containers

hash_map <int, int> hmap1, hmap2, hmap3;

// hash_map iterators

hash_map <int, int>::iterator hmap1_Iter, hmap2_Iter, hmap3_Iter;

int i;

typedef pair <int, int> Int_Pair;

 

// insert data

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

{

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

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

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

}

 

// do some operations

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

cout<<"hmap1 hash_map data: ";

for(hmap1_Iter = hmap1.begin(); hmap1_Iter != hmap1.end(); hmap1_Iter++)

cout<<hmap1_Iter->second<<" ";

cout<<endl;

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

cout<<"hmap2 hash_map data: ";

for(hmap2_Iter = hmap2.begin(); hmap2_Iter != hmap2.end(); hmap2_Iter++)

cout<<hmap2_Iter->second<<" ";

cout<<endl;

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

cout<<"hmap3 hash_map data: ";

for(hmap3_Iter = hmap3.begin(); hmap3_Iter != hmap3.end(); hmap3_Iter++)

cout<<hmap3_Iter->second<<" ";

cout<<endl;

// more operations

cout<<"\nOperation: hmap1 != hmap2?"<<endl;

if (hmap1 != hmap2)

cout<<"The hash_maps hmap1 and hmap2 are not equal."<<endl;

else

cout<<"The hash_maps hmap1 and hmap2 are equal."<<endl;

cout<<"\nOperation: hmap1 != hmap3?"<<endl;

if (hmap1 != hmap3)

cout<<"The hash_maps hmap1 and hmap3 are not equal."<<endl;

else

cout<<"The hash_maps hmap1 and hmap3 are equal."<<endl;

return 0;

}

 

Output examples:

 

Operation: hash_map <int, int> hmap1

hmap1 hash_map data: 0 1 2

Operation: hash_map <int, int> hmap2

hmap2 hash_map data: 0 1 4

Operation: hash_map <int, int> hmap3

hmap3 hash_map data: 0 1 2

Operation: hmap1 != hmap2?

The hash_maps hmap1 and hmap2 are not equal.

Operation: hmap1 != hmap3?

The hash_maps hmap1 and hmap3 are equal.

Press any key to continue . . .

 

 

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