CppDS.com

C++ 98 11 14 17 20 手册

std::multimap<Key,T,Compare,Allocator>::insert

来自cppreference.com
< cpp‎ | container‎ | multimap
iterator insert( const value_type& value );
(1)
iterator insert( value_type&& value );
(1) (C++17 起)
template< class P >
iterator insert( P&& value );
(2) (C++11 起)
(3)
iterator insert( iterator hint, const value_type& value );
(C++11 前)
iterator insert( const_iterator hint, const value_type& value );
(C++11 起)
iterator insert( const_iterator hint, value_type&& value );
(3) (C++17 起)
template< class P >
iterator insert( const_iterator hint, P&& value );
(4) (C++11 起)
template< class InputIt >
void insert( InputIt first, InputIt last );
(5)
void insert( std::initializer_list<value_type> ilist );
(6) (C++11 起)
iterator insert(node_type&& nh);
(7) (C++17 起)
iterator insert(const_iterator hint, node_type&& nh);
(8) (C++17 起)

插入元素到容器中。

1-2) 插入 value 。若容器拥有带等价关键的元素,则插入到范围的上界。(C++11 起)重载 (2) 等价于 emplace(std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。
3-4) 插入 value 到尽可能接近,恰好前于(C++11 起) hint 的位置。重载 (4) 等价于 emplace_hint(hint, std::forward<P>(value)) ,且仅若 std::is_constructible<value_type, P&&>::value == true 才参与重载决议。
5) 插入来自范围 [first, last) 的元素。
6) 插入来自 initializer_list ilist 的元素。
7)nh 是空的结点把柄,则不做任何事。否则插入 nh 所占有的元素到容器并返回指向被插入元素的迭代器。若范围含有关键等价于存在于容器中的 nh.key() 的关键,则在范围结尾插入元素。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。
8)nh 是空的结点把柄,则不做任何事并返回尾迭代器。否则,插入 nh 所占有的元素到容器,并返回指向拥有等于 nh.key() 的关键的元素的迭代器元素被插入到尽可能接近正好先于 hint 的位置。若 nh 非空且 get_allocator() != nh.get_allocator() 则行为未定义。

没有迭代器或引用被非法化。若插入成功,则在结点把柄保有元素时获得的指向该元素的指针和引用被非法化,而在提取前获得的指向元素的指针和引用变得合法。 (C++17 起)

参数

hint -

用作搜索开始位置的建议的迭代器

(C++11 前)

指向将插入新元素到其前的位置的迭代器

(C++11 起)
value - 要插入的值
first, last - 要插入的元素范围
ilist - 插入值来源的 initializer_list
nh - 兼容的结点把柄
类型要求
-
InputIt 必须满足遗留输入迭代器 (LegacyInputIterator) 的要求。

返回值

1-4) 指向被插入元素的迭代器。
5-6) (无)
7,8)nh 为空则为尾迭代器,否则为指向被插入元素的迭代器。

异常

1-4) 若任何操作抛出异常,则插入无效果。

复杂度

1-2) 与容器大小成对数, O(log(size()))
3-4) 若插入恰好发生在 hint 的位置则为均摊常数,否则与容器大小成对数。
(C++11 前)
3-4) 若插入恰好发生在 hint 的位置则为均摊常数,否则与容器大小成对数。
(C++11 起)
5-6) O(N*log(size() + N)) ,其中 N 是要插入的元素数。
7) 与容器大小成对数, O(log(size()))
8) 若插入恰好发生在 hint 的位置则为均摊常数,否则与容器大小成对数。

示例

#include <iostream>
#include <string>
#include <map>
#include <functional>
 
template<class M>
void print(const M& mmap)
{
    for (auto & e : mmap)
        std::cout << "{" << e.first << "," << e.second << "} ";
    std::cout << '\n';
}
 
int main()
{
  // 列表初始化
  std::multimap<int, std::string, std::greater<int>> mmap 
    {{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
 
  // 用 value_type 插入
  mmap.insert(decltype(mmap)::value_type(5, "pqr"));
  print(mmap);
 
  // 用 make_pair 插入
  mmap.insert(std::make_pair(6, "uvw"));
  print(mmap);
 
  mmap.insert({7, "xyz"});
  print(mmap);
 
  // 用 initialization_list 插入
  mmap.insert({{5, "one"}, {5, "two"}});
  print(mmap);
 
  // 擦除所有关键为 5 的条目
  mmap.erase(5);
  print(mmap);
 
  // 寻找并擦除特定条目
  auto pos = mmap.begin();
  while (pos->second != "bar" && pos != mmap.end()) ++pos;
  mmap.erase(pos);
  print(mmap);
}

输出:

{5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc} 
{6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc} 
{7,xyz} {6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc} 
{7,xyz} {6,uvw} {5,def} {5,pqr} {5,one} {5,two} {3,baz} {2,foo} {2,bar} {1,abc} 
{7,xyz} {6,uvw} {3,baz} {2,foo} {2,bar} {1,abc} 
{7,xyz} {6,uvw} {3,baz} {2,foo} {1,abc}

参阅

(C++11)
原位构造元素
(公开成员函数)
使用提示原位构造元素
(公开成员函数)
关闭