unordered_set::insert
Adds elements.
std::pair<iterator, bool> insert(const value_type& val);
iterator insert(iterator where, const value_type& val);
template<class InIt>
void insert(InIt first, InIt last);
Parameters
InIt
The iterator type.first
Beginning of range to insert.last
End of range to insert.val
Value to insert.where
Where in container to insert (hint only).
Remarks
The first member function determines whether an element X exists in the sequence whose key has equivalent ordering to that of val. If not, it creates such an element X and initializes it with val. The function then determines the iterator where that designates X. If an insertion occurred, the function returns std::pair(where, true). Otherwise, it returns std::pair(where, false).
The second member function returns insert(val).first, using where as a starting place within the controlled sequence to search for the insertion point. (Insertion can possibly occur somewhat faster, if the insertion point immediately precedes or follows where.) The third member function inserts the sequence of element values, for each where in the range [first, last), by calling insert(*where).
If an exception is thrown during the insertion of a single element, the container is left unaltered and the exception is rethrown. If an exception is thrown during the insertion of multiple elements, the container is left in a stable but unspecified state and the exception is rethrown.
Example
// std_tr1__unordered_set__unordered_set_insert.cpp
// compile with: /EHsc
#include <unordered_set>
#include <iostream>
typedef std::tr1::unordered_set<char> Myset;
int main()
{
Myset c1;
c1.insert('a');
c1.insert('b');
c1.insert('c');
// display contents " [c] [b] [a]"
for (Myset::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << *it << "]";
std::cout << std::endl;
// insert with hint and reinspect
Myset::iterator it2 = c1.insert(c1.begin(), 'd');
for (Myset::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << *it << "]";
std::cout << std::endl;
// insert range and inspect
Myset c2;
c2.insert(c1.begin(), c1.end());
for (Myset::const_iterator it = c2.begin();
it != c2.end(); ++it)
std::cout << " [" << *it << "]";
std::cout << std::endl;
// insert with checking and reinspect
std::pair<Myset::iterator, bool> pib =
c1.insert('e');
std::cout << "insert(['a',]) success == "
<< std::boolalpha << pib.second << std::endl;
pib = c1.insert('a');
std::cout << "insert(['a',]) success == "
<< std::boolalpha << pib.second << std::endl;
for (Myset::const_iterator it = c1.begin();
it != c1.end(); ++it)
std::cout << " [" << *it << "]";
std::cout << std::endl;
return (0);
}
[c] [b] [a] [d] [c] [b] [a] [d] [c] [b] [a] insert(['a',]) success == true insert(['a',]) success == false [e] [d] [c] [b] [a]
Requirements
Header: <unordered_set>
Namespace: std::tr1