C++ STL hash_map, constructor 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++ hash_map to construct a hash_map that is empty or that is a copy of all or part of some other hash_map in C++ programming

To show: How to use the C++ hash_map, constructor to construct a hash_map that is empty or that is a copy of all or part of some other hash_map in C++ programming

 

// C++ STL hash_map, constructor

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

{

typedef pair <int, int> Int_Pair;

// hash_map iterators

hash_map <int, int>::iterator hmp0_Iter, hmp1_Iter, hmp3_Iter, hmp4_Iter, hmp5_Iter, hmp6_Iter;

hash_map <int, int, hash_compare<int, greater<int> > >::iterator hmp2_Iter;

// create an empty hash_map hmp0 of key type integer

hash_map <int, int> hmp0;

// create an empty hash_map hmp1 with the key comparison function of less than, then insert 4 elements

hash_map <int, int, hash_compare <int, less<int> > > hmp1;

hmp1.insert(Int_Pair(1, 13));

hmp1.insert(Int_Pair(3, 51));

hmp1.insert(Int_Pair(7, 22));

hmp1.insert(Int_Pair(2, 31));

// create an empty hash_map hmp2 with the key comparison function of greater than, then insert 4 elements, no duplicate key

hash_map <int, int, hash_compare <int, greater<int> > > hmp2;

hmp2.insert(Int_Pair(1, 17));

hmp2.insert(Int_Pair(2, 20));

hmp2.insert(Int_Pair(4, 13));

hmp2.insert(Int_Pair(3, 34));

// create a hash_map hmp3 with the hash_map hmp1 allocator, notice the duplicate key

hash_map <int, int>::allocator_type hmp1_Alloc;

hmp1_Alloc = hmp1.get_allocator();

hash_map <int, int> hmp3(less<int>(), hmp1_Alloc);

hmp3.insert(Int_Pair(2, 17));

hmp3.insert(Int_Pair(1, 12));

hmp3.insert(Int_Pair(2, 15));

hmp3.insert(Int_Pair(1, 22));

// create a hash_map hm5 by copying the range hm1[_First, _Last)

hash_map <int, int>::const_iterator hmp1_PIter, hmp1_QIter;

hmp1_PIter = hmp1.begin( );

hmp1_QIter = hmp1.begin( );

hmp1_QIter++;

hmp1_QIter++;

hash_map <int, int> hmp5(hmp1_PIter, hmp1_QIter);

// create a hash_map hm6 by copying the range hm2[_First, _Last) and with the allocator of hash_map hm2

hash_map <int, int>::allocator_type hmp2_Alloc;

hmp2_Alloc = hmp2.get_allocator();

hash_map <int, int> hmp6(hmp2.begin(), ++hmp2.begin(), less<int>(), hmp2_Alloc);

// more operations

cout<<"Operation: hash_map <int, int> hmp0;"<<endl;

cout<<"hmp0 hash_map data: ";

for(hmp0_Iter = hmp0.begin(); hmp0_Iter != hmp0.end(); hmp0_Iter++)

cout<<hmp0_Iter->second<<" ";

cout<<endl;

cout<<"\nOperation: hash_map<int, int, hash_compare<int, less<int> > > hmp1;"<<endl;

cout<<"Operation2: hmp1.insert(Int_Pair(1, 13))..."<<endl;

cout<<"hmp1 hash_map data: ";

for(hmp1_Iter = hmp1.begin(); hmp1_Iter != hmp1.end(); hmp1_Iter++)

cout<<hmp1_Iter->second<<" ";

cout<<endl;

cout<<"\nOperation: hash_map<int, int, hash_compare<int, greater<int> > > hmp2;"<<endl;

cout<<"Operation2: hmp2.insert(Int_Pair(1, 17))..."<<endl;

cout<<"hmp2 hash_map data: ";

for(hmp2_Iter = hmp2.begin(); hmp2_Iter != hmp2.end(); hmp2_Iter++)

cout<<hmp2_Iter->second<<" ";

cout<<endl;

cout<<"\nOperation: hash_map<int, int, int> hmp3(less<int>(), hmp1_Alloc);"<<endl;

cout<<"Operation2: hmp3.insert(Int_Pair(2, 17))..."<<endl;

cout<<"hmp3 hash_map data: ";

for(hmp3_Iter = hmp3.begin(); hmp3_Iter != hmp3.end(); hmp3_Iter++)

cout<<hmp3_Iter->second<<" ";

cout<<endl;

cout<<"\nOperation: hash_map<int, int> hmp5(hmp1_PIter, hmp1_QIter);"<<endl;

cout<<"hmp5 hash_map data: ";

for(hmp5_Iter = hmp5.begin(); hmp5_Iter != hmp5.end(); hmp5_Iter++)

cout<<hmp5_Iter->second<<" ";

cout<<endl;

cout<<"\nOperation: hash_map<int, int> hmp6(hmp2.begin(), ++hmp2.begin(), less<int>(), hmp2_Alloc);"<<endl;

cout<<"hmp6 hash_map data: ";

for(hmp6_Iter = hmp6.begin(); hmp6_Iter != hmp6.end(); hmp6_Iter++)

cout<<hmp6_Iter->second <<" ";

cout<<endl;

return 0;

}

 

Output example:

 

Operation: hash_map <int, int> hmp0;

hmp0 hash_map data:

Operation: hash_map<int, int, hash_compare<int, less<int> > > hmp1;

Operation2: hmp1.insert(Int_Pair(1, 13))...

hmp1 hash_map data: 13 31 51 22

Operation: hash_map<int, int, hash_compare<int, greater<int> > > hmp2;

Operation2: hmp2.insert(Int_Pair(1, 17))...

hmp2 hash_map data: 17 20 34 13

Operation: hash_map<int, int, int> hmp3(less<int>(), hmp1_Alloc);

Operation2: hmp3.insert(Int_Pair(2, 17))...

hmp3 hash_map data: 12 17

Operation: hash_map<int, int> hmp5(hmp1_PIter, hmp1_QIter);

hmp5 hash_map data: 13 31

Operation: hash_map<int, int> hmp6(hmp2.begin(), ++hmp2.begin(), less<int>(), hmp2_Alloc);

hmp6 hash_map data: 17

Press any key to continue . . .

 

 

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