std::unordered_set<Key,Hash,KeyEqual,Allocator>::insert
Min standard notice:
Inserts element(s) into the container, if the container doesn’t already contain an element with an equivalent key.
# Declarations
std::pair<iterator,bool> insert( const value_type& value );
(since C++11)
std::pair<iterator,bool> insert( value_type&& value );
(since C++11)
iterator insert( const_iterator hint, const value_type& value );
(since C++11)
iterator insert( const_iterator hint, value_type&& value );
(since C++11)
template< class InputIt >
void insert( InputIt first, InputIt last );
(since C++11)
void insert( std::initializer_list<value_type> ilist );
(since C++11)
insert_return_type insert( node_type&& nh );
(since C++17)
iterator insert( const_iterator hint, node_type&& nh );
(since C++17)
template< class K >
std::pair<iterator, bool> insert( K&& obj );
(since C++23)
template< class K >
iterator insert( const_iterator hint, K&& obj );
(since C++23)
# Parameters
hint: iterator, used as a suggestion as to where to insert the contentvalue: element value to insertfirst, last: range of elements to insertilist: initializer list to insert the values fromnh: a compatible node handleobj: a value of any type that can be transparently compared with a key
# Notes
The hinted insert (3,4) does not return a boolean in order to be signature-compatible with positional insert on sequential containers, such as std::vector::insert. This makes it possible to create generic inserters such as std::inserter. One way to check success of a hinted insert is to compare size() before and after.
# Example
#include <array>
#include <iostream>
#include <unordered_set>
std::ostream& operator<<(std::ostream& os, std::unordered_set<int> const& s)
{
for (os << '[' << s.size() << "] { "; int i : s)
os << i << ' ';
return os << "}\n";
}
int main ()
{
std::unordered_set<int> nums{2, 3, 4};
std::cout << "1) Initially: " << nums << std::boolalpha;
auto p = nums.insert(1); // insert element, overload (1)
std::cout << "2) '1' was inserted: " << p.second << '\n';
std::cout << "3) After insertion: " << nums;
nums.insert(p.first, 0); // insert with hint, overload (3)
std::cout << "4) After insertion: " << nums;
std::array<int, 4> a = {10, 11, 12, 13};
nums.insert(a.begin(), a.end()); // insert range, overload (5)
std::cout << "5) After insertion: " << nums;
nums.insert({20, 21, 22, 23}); // insert initializer_list, (6)
std::cout << "6) After insertion: " << nums;
std::unordered_set<int> other_nums = {42, 43};
auto node = other_nums.extract(other_nums.find(42));
nums.insert(std::move(node)); // insert node, overload (7)
std::cout << "7) After insertion: " << nums;
node = other_nums.extract(other_nums.find(43));
nums.insert(nums.begin(), std::move(node)); // insert node with hint, (8)
std::cout << "8) After insertion: " << nums;
}