hash_set::insert
Inserts an element or a range of elements into a hash_set.
pair<iterator, bool> insert(
const value_type& _Val
);
iterator insert(
iterator _Where,
const value_type& _Val
);
template<class InputIterator>
void insert(
InputIterator _First,
InputIterator _Last
);
Parameters
_Val
The value of an element to be inserted into the hash_set unless the hash_set already contains that element or, more generally, an element whose key is equivalently ordered._Where
The place to start searching for the correct point of insertion. (Insertion can occur in amortized constant time, instead of logarithmic time, if the insertion point immediately follows _Where.)_First
The position of the first element to be copied from a hash_set._Last
The position just beyond the last element to be copied from a hash_set.
Return Value
The first insert member function returns a pair whose bool component returns true if an insertion was make and false if the hash_set already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.
To access the iterator component of a pair pr returned by this member function, use pr.first and to dereference it, use *(pr.first). To access the bool component of a pair pr returned by this member function, use pr.second, and to dereference it, use *(pr.second).
The second insert member function returns an iterator that points to the position where the new element was inserted into the hash_set.
Remarks
The third member function inserts the sequence of element values into a hash_set corresponding to each element addressed by an iterator of in the range [_First, _Last) of a specified hash_set.
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. See The stdext Namespace for more information.
Example
// hash_set_insert.cpp
// compile with: /EHsc
#include <hash_set>
#include <iostream>
int main( )
{
using namespace std;
using namespace stdext;
hash_set <int>::iterator hs1_pIter, hs2_pIter;
hash_set <int, hash_compare <int, less<int> > > hs1, hs2;
hs1.insert( 10 );
hs1.insert( 20 );
hs1.insert( 30 );
hs1.insert( 40 );
cout << "The original hs1 =";
for ( hs1_pIter = hs1.begin( ); hs1_pIter != hs1.end( );
hs1_pIter++ )
cout << " " << *hs1_pIter;
cout << "." << endl;
pair< hash_set<int>::iterator, bool > pr;
pr = hs1.insert( 10 );
if(pr.second == true)
{
cout << "The element 10 was inserted in hs1 successfully."
<< endl;
}
else
{
cout << "The element 10 already exists in hs1 and"
<< " *( pr.first ) = " << *( pr.first ) << "."
<< endl;
}
hs1.insert( --hs1.end( ), 50 );
cout << "After the insertions, hs1 =";
for ( hs1_pIter = hs1.begin( ); hs1_pIter != hs1.end( );
hs1_pIter++ )
cout << " " << *hs1_pIter;
cout << "." << endl;
hs2.insert( 100 );
hs2.insert( ++hs1.begin( ), --hs1.end( ) );
cout << "hs2 =";
for ( hs2_pIter = hs2.begin( ); hs2_pIter != hs2.end( );
hs2_pIter++ )
cout << " " << *hs2_pIter;
cout << "." << endl;
}
The original hs1 = 40 10 20 30.
The element 10 already exists in hs1 and *( pr.first ) = 10.
After the insertions, hs1 = 40 10 50 20 30.
hs2 = 10 50 20 100.
Requirements
Header: <hash_set>
Namespace: stdext