Partager via


hash_set::hash_set

Constructs a hash_set that is empty or that is a copy of all or part of some other hash_set.

hash_set( );
explicit hash_set(
   const Traits& _Comp
);
hash_set(
   const Traits& _Comp,
   const Allocator& _Al
);
hash_set(
   const hash_set<Key, Traits, Allocator>& _Right
);
template<class InputIterator>
   hash_set(
      InputIterator _First,
      InputIterator _Last
   );
template<class InputIterator>
   hash_set(
      InputIterator _First,
      InputIterator _Last,
      const Traits& _Comp
   );
template<class InputIterator>
   hash_set(
      InputIterator _First,
      InputIterator _Last,
      const Traits& _Comp,
      const Allocator& _Al
   );
hash_set(
   hash_set&& _Right
);

Parameters

Parameter

Description

_Al

The storage allocator class to be used for this hash_set object, which defaults to Allocator.

_Comp

The comparison function of type const Traits used to order the elements in the hash_set, which defaults to hash_compare.

_Right

The hash_set of which the constructed hash_set is to be a copy.

_First

The position of the first element in the range of elements to be copied.

_Last

The position of the first element beyond the range of elements to be copied.

Remarks

All constructors store a type of allocator object that manages memory storage for the hash_set and that can later be returned by calling hash_set::get_allocator. The allocator parameter is often omitted in the class declarations and preprocessing macros used to substitute alternative allocators.

All constructors initialize their hash_sets.

All constructors store a function object of type Traits that is used to establish an order among the keys of the hash_set and that can later be returned by calling hash_set::key_comp. For more information on Traits see the hash_set Class topic.

The next three constructors specify an empty initial hash_set, the second specifying the type of comparison function (_Comp) to be used in establishing the order of the elements and the third explicitly specifying the allocator type (_Al) to be used. The key word explicit suppresses certain kinds of automatic type conversion.

The fourth constructor specifies a copy of the hash_set _Right.

The last three constructors copy the range [_First, _Last) of a hash_set with increasing explicitness in specifying the type of comparison function of class Traits and allocator.

The last constructor moves the hash_set _Right.

The actual order of elements in a hash_set container depends on the hash function, the ordering function and the current size of the hash table and cannot, in general, be predicted as it could with the set container, where it was determined by the ordering function alone.

Example

// hash_set_hash_set.cpp
// compile with: /EHsc
#include <hash_set>
#include <iostream>

int main( )
{
   using namespace std;
   using namespace stdext;
   hash_set <int>::iterator hs1_Iter, hs3_Iter, hs4_Iter,
      hs5_Iter, hs6_Iter, hs7_Iter;
   hash_set <int, hash_compare <int, greater<int> > >::iterator
      hs2_Iter;

   // Create an empty hash_set hs0 of key type integer
   hash_set <int> hs0;

   // Create an empty hash_set hs1 with the key comparison
   // function of less than, then insert 4 elements
   hash_set <int, hash_compare <int, less<int> > > hs1;
   hs1.insert( 10 );
   hs1.insert( 20 );
   hs1.insert( 30 );
   hs1.insert( 40 );

   // Create an empty hash_set hs2 with the key comparison
   // function of geater than, then insert 2 elements
   hash_set <int, hash_compare <int, greater<int> > > hs2;
   hs2.insert(10);
   hs2.insert(20);

   // Create a hash_set hs3 with the 
   // allocator of hash_set hs1
   hash_set <int>::allocator_type hs1_Alloc;
   hs1_Alloc = hs1.get_allocator( );
   hash_set <int> hs3( hash_compare <int, less<int> >( ),
      hs1_Alloc );
   hs3.insert( 30 );

   // Create a copy, hash_set hs4, of hash_set hs1
   hash_set <int> hs4( hs1 );

   // Create a hash_set hs5 by copying the range hs1[_First, _Last)
   hash_set <int>::const_iterator hs1_bcIter, hs1_ecIter;
   hs1_bcIter = hs1.begin( );
   hs1_ecIter = hs1.begin( );
   hs1_ecIter++;
   hs1_ecIter++;
   hash_set <int> hs5( hs1_bcIter, hs1_ecIter );

   // Create a hash_set hs6 by copying the range hs4[_First, _Last)
   // and with the allocator of hash_set hs2
   hash_set <int>::allocator_type hs2_Alloc;
   hs2_Alloc = hs2.get_allocator( );
   hash_set <int> hs6( hs4.begin( ), ++hs4.begin( ), 
      less<int>( ), hs2_Alloc );

   cout << "hs1 = ";
   for ( hs1_Iter = hs1.begin( ); hs1_Iter != hs1.end( );
         hs1_Iter++ )
      cout << *hs1_Iter << " ";
   cout << endl;
   
   cout << "hs2 = " ;
   for ( hs2_Iter = hs2.begin( ); hs2_Iter != hs2.end( );
         hs2_Iter++ )
      cout << *hs2_Iter << " ";
   cout << endl;

   cout << "hs3 = ";
   for ( hs3_Iter = hs3.begin( ); hs3_Iter != hs3.end( );
         hs3_Iter++ )
      cout << *hs3_Iter << " ";
   cout << endl;

   cout << "hs4 = ";
   for ( hs4_Iter = hs4.begin( ); hs4_Iter != hs4.end( );
         hs4_Iter++ )
      cout << *hs4_Iter << " ";
   cout << endl;

   cout << "hs5 = ";
   for ( hs5_Iter = hs5.begin( ); hs5_Iter != hs5.end( );
         hs5_Iter++ )
      cout << *hs5_Iter << " ";
   cout << endl;

   cout << "hs6 = ";
   for ( hs6_Iter = hs6.begin( ); hs6_Iter != hs6.end( );
         hs6_Iter++ )
      cout << *hs6_Iter << " ";
   cout << endl;

    // Create a copy, hash_set hs7, of hash_set hs1 by moving
    hash_set <int, hash_compare <int, less<int> > >
        hs7(move(hs1);
    cout << "hs7 =";
    for (hs7_Iter = hs7.begin(); hs7_Iter != hs7.end(); hs7_Iter++)
        cout << " " << hs7_Iter -> second;
    cout << endl;
}

Output

hs1 = 40 10 20 30 
hs2 = 10 20 
hs3 = 30 
hs4 = 40 10 20 30 
hs5 = 40 10 
hs6 = 40 
hs7 = 40 10 20 30 

Requirements

Header: <hash_set>

Namespace: stdext

See Also

Reference

hash_set Class

Standard Template Library

Other Resources

hash_set Members