C++ STL hash_multimap, 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_multimap to construct a list of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other hash_multimap in C++ programming

To show: How to use the C++ hash_multimap, constructor to construct a list of a specific size or with elements of a specific value or with a specific allocator or as a copy of some other hash_multimap in C++ programming

 

// C++ STL hash_multimap, 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;

// iterators

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

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

// create an empty hash_multimap hmp0 of key type integer

hash_multimap <int, int> hmp0;

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

cout<<"Inserting data into hash_multimap..."<<endl;

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

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

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

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

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

hmp1.insert(Int_Pair(4, 9));

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

// create an empty hash_multimap hmp2 with the key comparison function of greater than, then insert 2 elements

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

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

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

// create a hash_multimap hmp3 with the allocator of hash_multimap hmp1

hash_multimap <int, int>::allocator_type hmp1_Alloc;

hmp1_Alloc = hmp1.get_allocator();

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

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

hmp3.insert(Int_Pair(4, 10));

// create a hash_multimap hmp4 by copying the range hmp1[_First, _Last)

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

hmp1_PIter = hmp1.begin();

hmp1_QIter = hmp1.begin();

hmp1_QIter++;

hmp1_QIter++;

hmp1_QIter++;

hash_multimap <int, int> hmp4(hmp1_PIter, hmp1_QIter);

// create a hash_multimap hmp5 by copying the range hmp2[_First, _Last) and with the allocator of hash_multimap hmp2

hash_multimap <int, int>::allocator_type hmp2_Alloc;

hmp2_Alloc = hmp2.get_allocator();

hash_multimap <int, int> hmp5(hmp2.begin(), ++hmp2.begin(), less<int>(), hmp2_Alloc);

// do some operations

cout<<"Operation: hash_multimap <int> hmp0"<<endl;

cout<<"hmp0 hash_multimap data: ";

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

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

cout<<endl;

cout<<"\nOperation1: hash_multimap<int, int, hash_compare<int, less<int> > > hmp1;"<<endl;

cout<<"Operation2: hmp1.insert(Int_Pair(3, 12))..."<<endl;

cout<<"hmp1 hash_multimap data: ";

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

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

cout<<endl;

cout<<"\nOperation1: hash_multimap<int, int, hash_compare<int, greater<int> > > hmp2;"<<endl;

cout<<"Operation2: hmp2.insert(Int_Pair(2, 13))..."<<endl;

cout<<"hmp2 hash_multimap data: ";

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

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

cout<<endl;

cout<<"\nOperation1: hash_multimap<int> hmp3(less<int>(), hmp1_Alloc);"<<endl;

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

cout<<"hmp3 hash_multimap data: ";

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

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

cout<<endl;

cout<<"\nOperation: hash_multimap<int> hmp4(hmp1_PIter, hmp1_QIter);"<<endl;

cout<<"hmp4 hash_multimap data: ";

for(hmp4_Iter = hmp4.begin(); hmp4_Iter != hmp4.end(); hmp4_Iter++)

cout<<hmp4_Iter->second<<" ";

cout<<endl;

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

cout<<"hmp5 hash_multimap data: ";

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

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

cout<<endl;

return 0;

}

 

Output examples:

 

Inserting data into hash_multimap...

Operation: hash_multimap <int> hmp0

hmp0 hash_multimap data:

Operation1: hash_multimap<int, int, hash_compare<int, less<int> > > hmp1;

Operation2: hmp1.insert(Int_Pair(3, 12))...

hmp1 hash_multimap data: 22 30 12 9 41 30

Operation1: hash_multimap<int, int, hash_compare<int, greater<int> > > hmp2;

Operation2: hmp2.insert(Int_Pair(2, 13))...

hmp2 hash_multimap data: 17 13

Operation1: hash_multimap<int> hmp3(less<int>(), hmp1_Alloc);

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

hmp3 hash_multimap data: 13 10

Operation: hash_multimap<int> hmp4(hmp1_PIter, hmp1_QIter);

hmp4 hash_multimap data: 22 30 12

Operation: hash_multimap<int> hmp5(hmp2.begin(), ++hmp2.begin(), less<int>(), hmp2_Alloc);

hmp5 hash_multimap data: 17

Press any key to continue . . .

 

 

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