CARVIEW |
Select Language
HTTP/1.1 301 Moved Permanently
Server: nginx
Date: Sat, 19 Jul 2025 10:35:58 GMT
Content-Type: text/html; charset=UTF-8
Transfer-Encoding: chunked
Connection: keep-alive
Location: /reference/unordered_set/unordered_multiset/insert/
HTTP/1.1 200 OK
Server: nginx
Date: Sat, 19 Jul 2025 10:35:59 GMT
Content-Type: text/html; charset=utf-8
Transfer-Encoding: chunked
Connection: keep-alive
ETag: W/"98b6-I+7sTHGVFBUXrf0afSYpb86AKCc"
Content-Encoding: gzip
Inserts new elements in the unordered_multiset.
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:
Versions (5) and (6) return no value.
Member type iterator is a forward iterator type.
All iterators in an unordered_multiset 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_multiset 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_multiset
-
unordered_multiset::~unordered_multisetC++11
-
unordered_multiset::unordered_multisetC++11
member functions
-
unordered_multiset::beginC++11
-
unordered_multiset::bucketC++11
-
unordered_multiset::bucket_countC++11
-
unordered_multiset::bucket_sizeC++11
-
unordered_multiset::cbeginC++11
-
unordered_multiset::cendC++11
-
unordered_multiset::clearC++11
-
unordered_multiset::countC++11
-
unordered_multiset::emplaceC++11
-
unordered_multiset::emplace_hintC++11
-
unordered_multiset::emptyC++11
-
unordered_multiset::endC++11
-
unordered_multiset::equal_rangeC++11
-
unordered_multiset::eraseC++11
-
unordered_multiset::findC++11
-
unordered_multiset::get_allocatorC++11
-
unordered_multiset::hash_functionC++11
-
unordered_multiset::insertC++11
-
unordered_multiset::key_eqC++11
-
unordered_multiset::load_factorC++11
-
unordered_multiset::max_bucket_countC++11
-
unordered_multiset::max_load_factorC++11
-
unordered_multiset::max_sizeC++11
-
unordered_multiset::operator=C++11
-
unordered_multiset::rehashC++11
-
unordered_multiset::reserveC++11
-
unordered_multiset::sizeC++11
-
unordered_multiset::swapC++11
-
non-member overloads
public member function
<unordered_set>
std::unordered_multiset::insert
(1) | iterator insert ( const value_type& val ); |
---|---|
(2) | iterator 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
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_multiset 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_multiset 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_multiset as an alias of the first template parameter (Key).
Return value
In the versions returning a value ((1),(2),(3) and (4)), the function returns an iterator to the newly inserted element.Versions (5) and (6) return no value.
Member type iterator is a forward iterator type.
All iterators in an unordered_multiset 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:
myums contains: green blue reddish yellow red red 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_multiset container remain valid in all cases, even after a rehash.
See also
- unordered_multiset::emplace
- Construct and insert element (public member function)
- unordered_multiset::emplace_hint
- Construct and insert element with hint (public member function)
- unordered_multiset::operator=
- Assign content (public member function)
- unordered_multiset::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