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

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

 

// C++ STL hash_set, 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_set <int>::iterator hst0_Iter, hst1_Iter, hst3_Iter, hst4_Iter, hst5_Iter;

hash_set <int, hash_compare <int, greater<int> > >::iterator hst2_Iter;

// create an empty hash_set hst0 of key type integer

hash_set <int> hst0;

cout<<"Inserting data into the hash_set...."<<endl;

// create an empty hash_set hst1 with the key comparison function of less than, then insert 5 elements

hash_set <int, hash_compare<int, less<int> > > hst1;

hst1.insert(7);

hst1.insert(3);

hst1.insert(12);

hst1.insert(51);

hst1.insert(10);

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

hash_set<int, hash_compare<int, greater<int> > > hst2;

hst2.insert(71);

hst2.insert(68);

hst2.insert(68);

hst2.insert(55);

// create a hash_set hst3 with the hash_set hst1 allocator

hash_set<int>::allocator_type hst1_Alloc;

hst1_Alloc = hst1.get_allocator();

hash_set<int> hst3(less<int>(),hst1_Alloc);

hst3.insert(12);

hst3.insert(13);

hst3.insert(12);

// create a hash_set hst4 by copying the range hst1[_First, _Last)

hash_set <int>::const_iterator hst1_PIter, hst1_QIter;

hst1_PIter = hst1.begin();

hst1_QIter = hst1.begin();

hst1_QIter++;

hst1_QIter++;

hash_set<int> hst4(hst1_PIter, hst1_QIter);

// create a hash_set hst5 by copying the range hst4[_First, _Last) and with the allocator of hash_set hst2

hash_set <int>::allocator_type hst2_Alloc;

hst2_Alloc = hst2.get_allocator();

hash_set <int> hst5(hst1.begin(), ++hst1.begin(), less<int>(), hst2_Alloc);

// some operations

cout<<"Operation: hash_set <int> hst0"<<endl;

cout<<"hst0 hash_set data: ";

for(hst0_Iter = hst0.begin(); hst0_Iter != hst0.end(); hst0_Iter++)

cout<<*hst0_Iter<<" ";

cout<<endl;

cout<<"\nOperation: hash_set <int, hash_compare<int, less<int> > > hst1"<<endl;

cout<<"Operation: hst1.insert(7)..."<<endl;

cout<< "hst1 hash_set data: ";

for(hst1_Iter = hst1.begin(); hst1_Iter != hst1.end(); hst1_Iter++)

cout<<*hst1_Iter << " ";

cout<<endl;

cout<<"\nOperation: hash_set <int, hash_compare<int, greater<int> > > hst2"<<endl;;

cout<<"Operation: hst2.insert(71)..."<<endl;;

cout<<"hst2 hash_set data: ";

for(hst2_Iter = hst2.begin(); hst2_Iter != hst2.end(); hst2_Iter++)

cout<<*hst2_Iter<<" ";

cout<<endl;

cout<<"\nOperation: hash_set<int> hst3(less<int>(),hst1_Alloc)"<<endl;

cout<<"Operation: hst3.insert(12)..."<<endl;

cout<<"hst3 hash_set data: ";

for(hst3_Iter = hst3.begin(); hst3_Iter != hst3.end(); hst3_Iter++)

cout<<*hst3_Iter<<" ";

cout<<endl;

cout<<"\nOperation: hash_set<int> hst4(hst1_PIter, hst1_QIter)"<<endl;

cout<<"hst4 hash_set data: ";

for(hst4_Iter = hst4.begin(); hst4_Iter != hst4.end(); hst4_Iter++)

cout<<*hst4_Iter<<" ";

cout<<endl;

cout<<"\nOperation: hash_set <int> hst5(hst1.begin(), ++hst1.begin(), less<int>(), hst2_Alloc)"<<endl;

cout<<"hst5 hash_set data: ";

for(hst5_Iter = hst5.begin(); hst5_Iter != hst5.end(); hst5_Iter++)

cout<<*hst5_Iter<<" ";

cout<<endl;

return 0;

}

 

Output examples:

 

Inserting data into the hash_set....

Operation: hash_set <int> hst0

hst0 hash_set data:

Operation: hash_set <int, hash_compare<int, less<int> > > hst1

Operation: hst1.insert(7)...

hst1 hash_set data: 10 3 51 12 7

Operation: hash_set <int, hash_compare<int, greater<int> > > hst2

Operation: hst2.insert(71)...

hst2 hash_set data: 68 71 55

Operation: hash_set<int> hst3(less<int>(),hst1_Alloc)

Operation: hst3.insert(12)...

hst3 hash_set data: 12 13

Operation: hash_set<int> hst4(hst1_PIter, hst1_QIter)

hst4 hash_set data: 10 3

Operation: hash_set <int> hst5(hst1.begin(), ++hst1.begin(), less<int>(), hst2_Alloc)

hst5 hash_set data: 10

Press any key to continue . . .

 

 

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