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

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


// C++ STL hash_multiset, 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_set>

#include <iostream>

using namespace std;

using namespace stdext;


int main(void)


// iterators

hash_multiset <int>::iterator hms0_Iter, hms1_Iter, hms3_Iter, hms4_Iter, hms5_Iter;

hash_multiset <int, hash_compare <int, greater<int> > >::iterator hms2_Iter;


// create an empty hash_multiset hms0 of key type integer

hash_multiset <int> hms0;

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

hash_multiset<int, hash_compare<int, less<int> > > hms1;






// create an empty hash_multiset hms2 with the key comparison function of greater than, then insert 4 elements

hash_multiset<int, hash_compare<int, greater<int> > > hms2;






// create a hash_multiset hms3 with the allocator of hash_multiset hms1

hash_multiset <int>::allocator_type hms1_Alloc;

hms1_Alloc = hms1.get_allocator();

hash_multiset <int> hms3(less<int>(), hms1_Alloc);





// create a hash_multiset hms4 by copying the range hms1[_First, _Last)

hash_multiset <int>::const_iterator hms1_PIter, hms1_QIter;

hms1_PIter = hms1.begin();

hms1_QIter = hms1.begin();




hash_multiset<int> hms4(hms1_PIter, hms1_QIter);


// create a hash_multiset hms5 by copying the range hms2[_First, _Last) and with the allocator of hash_multiset hms2

hash_multiset<int>::allocator_type hms2_Alloc;

hms2_Alloc = hms2.get_allocator( );

hash_multiset<int> hms5(hms2.begin(), ++hms2.begin(),less<int>(), hms2_Alloc);


// some operations

cout<<"Operation: hash_multiset <int> hms0"<<endl;

cout<<"hms0 hash_multiset data: ";

for(hms0_Iter = hms0.begin(); hms0_Iter != hms0.end(); hms0_Iter++)

cout<<*hms0_Iter<<" ";


cout<<"\nOperation1: hash_multiset<int, hash_compare<int, less<int> > > hms1"<<endl;

cout<<"Operation2: hms1.insert(12)..."<<endl;

cout<<"hms1 hash_multiset data: ";

for(hms1_Iter = hms1.begin(); hms1_Iter != hms1.end(); hms1_Iter++)

cout<<*hms1_Iter<<" ";


cout<<"\nOperation1: hash_multiset<int, hash_compare<int, greater<int> > > hms2"<<endl;

cout<<"Operation2: hms2.insert(21)..."<<endl;;

cout<<"hms2 hash_multiset data: ";

for(hms2_Iter = hms2.begin(); hms2_Iter != hms2.end(); hms2_Iter++)

cout<<*hms2_Iter<<" ";


cout<<"\nOperation1: hash_multiset<int> hms3(less<int>(),hms1_Alloc)"<<endl;;

cout<<"Operation2: hms3.insert(71)..."<<endl;;

cout<<"hms3 hash_multiset data: ";

for(hms3_Iter = hms3.begin(); hms3_Iter != hms3.end(); hms3_Iter++)

cout<<*hms3_Iter<<" ";


cout<<"\nOperation: hash_multiset<int> hms4(hms1_PIter, hms1_QIter)"<<endl;;

cout<<"hms4 hash_multiset data: ";

for(hms4_Iter = hms4.begin(); hms4_Iter != hms4.end(); hms4_Iter++)

cout<<*hms4_Iter<<" ";


cout<<"\nOperation: hash_multiset<int> hms5(hms2.begin(), ++hms2.begin(), less<int>(), hms2_Alloc)"<<endl;;

cout<<"hms5 hash_multiset data: ";

for(hms5_Iter = hms5.begin(); hms5_Iter != hms5.end(); hms5_Iter++)

cout<<*hms5_Iter<<" ";


return 0;



Output examples:


Operation: hash_multiset <int> hms0

hms0 hash_multiset data:

Operation1: hash_multiset<int, hash_compare<int, less<int> > > hms1

Operation2: hms1.insert(12)...

hms1 hash_multiset data: 24 9 17 17 12

Operation1: hash_multiset<int, hash_compare<int, greater<int> > > hms2

Operation2: hms2.insert(21)...

hms2 hash_multiset data: 17 34 21 21

Operation1: hash_multiset<int> hms3(less<int>(),hms1_Alloc)

Operation2: hms3.insert(71)...

hms3 hash_multiset data: 52 31 71

Operation: hash_multiset<int> hms4(hms1_PIter, hms1_QIter)

hms4 hash_multiset data: 24 9 17

Operation: hash_multiset<int> hms5(hms2.begin(), ++hms2.begin(), less<int>(), hms2_Alloc)

hms5 hash_multiset data: 17

Press any key to continue . . .



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