CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 19 Jul 2025 14:23:24 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/unordered_set/unordered_set/insert/
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 19 Jul 2025 14:23:24 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"9857-tDTARrTrTarjzgL1eOil3tgSpXs"
Content-Encoding: gzip
Inserts new elements in the unordered_set.
Each element is inserted only if it is not equivalent to any other element already in the container (elements in an unordered_set have unique values).
This effectively increases the container size by the number of elements inserted.
The parameters determine how many elements are inserted and to which values they are initialized:
In versions (3) and (4), the function returns an iterator pointing either to the newly inserted element in the container or to the element whose key is equivalent.
Versions (5) and (6) return no value.
Member type iterator is a forward iterator type.
All iterators in an unordered_set have const access to the elements: Elements can be inserted or removed, but not modified while in the container.
The storage for the new element is allocated using allocator_traits<allocator_type>::construct(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).
Possible output:
A rehash is forced if the new container size after the insertion operation would increase above its capacity threshold (calculated as the container's bucket_count multiplied by its max_load_factor).
References to elements in the unordered_set container remain valid in all cases, even after a rehash.
Reference
C library:
- <cassert> (assert.h)
- <cctype> (ctype.h)
- <cerrno> (errno.h)
-
<cfenv> (fenv.h)C++11
- <cfloat> (float.h)
-
<cinttypes> (inttypes.h)C++11
- <ciso646> (iso646.h)
- <climits> (limits.h)
- <clocale> (locale.h)
- <cmath> (math.h)
- <csetjmp> (setjmp.h)
- <csignal> (signal.h)
- <cstdarg> (stdarg.h)
-
<cstdbool> (stdbool.h)C++11
- <cstddef> (stddef.h)
-
<cstdint> (stdint.h)C++11
- <cstdio> (stdio.h)
- <cstdlib> (stdlib.h)
- <cstring> (string.h)
-
<ctgmath> (tgmath.h)C++11
- <ctime> (time.h)
-
<cuchar> (uchar.h)C++11
- <cwchar> (wchar.h)
- <cwctype> (wctype.h)
Containers:
-
<array>C++11
- <deque>
-
<forward_list>C++11
- <list>
- <map>
- <queue>
- <set>
- <stack>
-
<unordered_map>C++11
-
<unordered_set>C++11
- <vector>
-
Input/Output:
Multi-threading:
-
<atomic>C++11
-
<condition_variable>C++11
-
<future>C++11
-
<mutex>C++11
-
<thread>C++11
-
Other:
- <algorithm>
- <bitset>
-
<chrono>C++11
-
<codecvt>C++11
- <complex>
- <exception>
- <functional>
-
<initializer_list>C++11
- <iterator>
- <limits>
- <locale>
- <memory>
- <new>
- <numeric>
-
<random>C++11
-
<ratio>C++11
-
<regex>C++11
- <stdexcept>
- <string>
-
<system_error>C++11
-
<tuple>C++11
-
<type_traits>C++11
-
<typeindex>C++11
- <typeinfo>
- <utility>
- <valarray>
<unordered_set>
-
unordered_multisetC++11
-
unordered_setC++11
unordered_set
-
unordered_set::~unordered_setC++11
-
unordered_set::unordered_setC++11
member functions
-
unordered_set::beginC++11
-
unordered_set::bucketC++11
-
unordered_set::bucket_countC++11
-
unordered_set::bucket_sizeC++11
-
unordered_set::cbeginC++11
-
unordered_set::cendC++11
-
unordered_set::clearC++11
-
unordered_set::countC++11
-
unordered_set::emplaceC++11
-
unordered_set::emplace_hintC++11
-
unordered_set::emptyC++11
-
unordered_set::endC++11
-
unordered_set::equal_rangeC++11
-
unordered_set::eraseC++11
-
unordered_set::findC++11
-
unordered_set::get_allocatorC++11
-
unordered_set::hash_functionC++11
-
unordered_set::insertC++11
-
unordered_set::key_eqC++11
-
unordered_set::load_factorC++11
-
unordered_set::max_bucket_countC++11
-
unordered_set::max_load_factorC++11
-
unordered_set::max_sizeC++11
-
unordered_set::operator=C++11
-
unordered_set::rehashC++11
-
unordered_set::reserveC++11
-
unordered_set::sizeC++11
-
unordered_set::swapC++11
-
non-member overloads
-
operators (unordered_set)C++11
-
swap (unordered_set)C++11
-
public member function
<unordered_set>
std::unordered_set::insert
(1) | pair<iterator,bool> insert ( const value_type& val ); |
---|---|
(2) | pair<iterator,bool> insert ( value_type&& val ); |
(3) | iterator insert ( const_iterator hint, const value_type& val ); |
(4) | iterator insert ( const_iterator hint, value_type&& val ); |
(5) | template <class InputIterator> void insert ( InputIterator first, InputIterator last ); |
(6) | void insert ( initializer_list<value_type> il ); |
Insert elements
Each element is inserted only if it is not equivalent to any other element already in the container (elements in an unordered_set have unique values).
This effectively increases the container size by the number of elements inserted.
The parameters determine how many elements are inserted and to which values they are initialized:
Parameters
- val
- Object to be copied to (or moved as) the value of the new element.
Versions (1) and (3) copy the element (i.e., val preserves its contents, the container keeps a copy).
Versions (2) and (4) move the element (i.e., val loses its content, which is acquired by the new element in the container).
Member type value_type is the type of the elements in the container, defined in unordered_set as an alias of the first template parameter (Key).
- hint
- Iterator to a position suggested as a hint on where to start the search for the proper insertion point. This value may or may not be used by the container to optimize the operation. The element will be stored in its corresponding bucket, no matter what is passed as hint.
Member type const_iterator is a forward iterator type. - first, last
- Iterators specifying a range of elements. Copies of the elements in the range [first,last) are inserted in the unordered_set container.
Notice that the range includes all the elements between first and last, including the element pointed by first but not the one pointed by last.
Neither first nor last shall be iterators in the destination container.
The template type can be any type of input iterator. - il
- An initializer_list object. The compiler will automatically construct such objects from initializer list declarators.
Member type value_type is the type of the elements contained in the container, defined in unordered_set as an alias of the first template parameter (Key).
Return value
In versions (1) and (2), the function returns a pair object whose first element is an iterator pointing either to the newly inserted element in the container or to the element whose key is equivalent, and a bool value indicating whether the element was successfully inserted or not.In versions (3) and (4), the function returns an iterator pointing either to the newly inserted element in the container or to the element whose key is equivalent.
Versions (5) and (6) return no value.
Member type iterator is a forward iterator type.
All iterators in an unordered_set have const access to the elements: Elements can be inserted or removed, but not modified while in the container.
The storage for the new element is allocated using allocator_traits<allocator_type>::construct(), which may throw exceptions on failure (for the default allocator, bad_alloc is thrown if the allocation request does not succeed).
Example
|
|
Possible output:
myset contains: green blue reddish white yellow black red orange purple |
Complexity
- Single element insertions:
- Average case: constant.
Worst case: linear in container size.
- Multiple elements insertion:
- Average case: linear in the number of elements inserted.
Worst case: N*(size+1): number of elements inserted times the container size plus one.
Iterator validity
On most cases, all iterators in the container remain valid after the insertion. The only exception being when the growth of the container forces a rehash. In this case, all iterators in the container are invalidated.A rehash is forced if the new container size after the insertion operation would increase above its capacity threshold (calculated as the container's bucket_count multiplied by its max_load_factor).
References to elements in the unordered_set container remain valid in all cases, even after a rehash.
See also
- unordered_set::emplace
- Construct and insert element (public member function)
- unordered_set::emplace_hint
- Construct and insert element with hint (public member function)
- unordered_set::operator=
- Assign content (public member function)
- unordered_set::erase
- Erase elements (public member function)
Home page | Privacy policy
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us
© cplusplus.com, 2000-2025 - All rights reserved - v3.3.4s
Spotted an error? contact us