public member function
<map>

std::multimap::insert

single element (1)
iterator insert (const value_type& val);
with hint (2)
iterator insert (iterator position, const value_type& val);
range (3)
template <class InputIterator>  void insert (InputIterator first, InputIterator last);
single element (1)
iterator insert (const value_type& val);template <class P> iterator insert (P&& val);
with hint (2)
iterator insert (const_iterator position, const value_type& val);template <class P> iterator insert (const_iterator position, P&& val);
range (3)
template <class InputIterator>  void insert (InputIterator first, InputIterator last);
initializer list (4)
void insert (initializer_list<value_type> il);
Insert element
Extends the container by inserting new elements, effectively increasing the container size by the number of elements inserted.

Internally, multimap containers keep all their elements sorted by key following the criterion specified by its comparison object. The elements are always inserted in its respective position following this ordering.

等價元素之間的相對順序沒有保證。
The relative ordering of elements with equivalent keys is preserved, and newly inserted elements follow those with equivalent keys already in the container.

引數決定了插入多少元素以及它們被初始化為哪個值。

引數

val
Value to be copied to (or moved as) the inserted element.
成員型別value_typeis the type of the elements in the container, defined in multimap aspair<const key_type,mapped_type>(see multimap member types).
The template parameterPshall be a type convertible tovalue_type.
接受 P&& 引數的簽名僅在 std::is_constructible<value_type,P&&>true 時呼叫。
如果已知Pis instantiated as a reference type, the argument is copied.
position
元素可插入位置的提示。
The function optimizes its insertion time if position points to the element that will precede the inserted element.
The function optimizes its insertion time if position points to the element that will follow the inserted element (or to the end, if it would be the last).
Notice that this is just a hint and does not force the new element to be inserted at that position within the multimap container (the elements in a multimap always follow a specific order depending on their key).
成員型別iteratorconst_iteratorare defined in multimap as bidirectional iterator types that point to elements.
first, last
指定元素範圍的迭代器。範圍中元素的副本[first,last)are inserted in the container.
請注意,範圍包括 first 和 last 之間的所有元素,包括 first 指向的元素,但不包括 last 指向的元素。
函式模板引數InputIteratorshall be an input iterator type that points to elements of a type from whichvalue_type物件的型別的元素。
il
An initializer_list object. Copies of these elements are inserted.
這些物件是從初始化列表宣告符自動構造的。
成員型別value_typeis the type of the elements contained in the container, defined in multimap aspair<const key_type,mapped_type>(see multimap member types).

返回值

In the versions returning a value, this is an iterator pointing to the newly inserted element in the multiset.

成員型別iterator是指向元素的雙向迭代器型別。

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
// multimap::insert (C++98)
#include <iostream>
#include <map>

int main ()
{
  std::multimap<char,int> mymultimap;
  std::multimap<char,int>::iterator it;

  // first insert function version (single parameter):
  mymultimap.insert ( std::pair<char,int>('a',100) );
  mymultimap.insert ( std::pair<char,int>('z',150) );
  it=mymultimap.insert ( std::pair<char,int>('b',75) );

  // second insert function version (with hint position):
  mymultimap.insert (it, std::pair<char,int>('c',300));  // max efficiency inserting
  mymultimap.insert (it, std::pair<char,int>('z',400));  // no max efficiency inserting

  // third insert function version (range insertion):
  std::multimap<char,int> anothermultimap;
  anothermultimap.insert(mymultimap.begin(),mymultimap.find('c'));

  // showing contents:
  std::cout << "mymultimap contains:\n";
  for (it=mymultimap.begin(); it!=mymultimap.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';

  std::cout << "anothermultimap contains:\n";
  for (it=anothermultimap.begin(); it!=anothermultimap.end(); ++it)
    std::cout << (*it).first << " => " << (*it).second << '\n';

  return 0;
}
輸出
mymultimap contains:
a => 100
b => 75
c => 300
z => 400
z => 150
anothermultimap contains:
a => 100
b => 75


複雜度

If a single element is inserted, logarithmic in size in general, but amortized constant if a hint is given and the position given is the optimal.

If N elements are inserted,Nlog(size+N)in general, but linear insize+Nif the elements are already sorted according to the same ordering criterion used by the container.
If N elements are inserted,Nlog(size+N).
Implementations may optimize if the range is already sorted.

迭代器有效性

沒有變化。

資料競爭

The container is modified.
Concurrently accessing existing elements is safe, although iterating ranges in the container is not.

異常安全

If a single element is to be inserted, there are no changes in the container in case of exception (strong guarantee).
Otherwise, the container is guaranteed to end in a valid state (basic guarantee).
如果不支援帶有元素構造適當引數的 allocator_traits::construct,或者指定了無效的position,則會導致未定義行為

另見