跳到主要内容

std::multimap insert() 方法

// (1) Non const version only
iterator insert( value_type&& value );

// (2) Non const version only
template< class P >
iterator insert( P&& value );

// (3) Non const version only
iterator insert( const_iterator hint, value_type&& value );

// (4) Non const version only
template< class P >
iterator insert( const_iterator hint, P&& value );

// (5) Non const version only
template< class InputIt >
void insert( InputIt first, InputIt last );

// (6) Non const version only
void insert( std::initializer_list<value_type> ilist );

// (7) Non const version only
iterator insert( node_type&& nh );

// (8) Non const version only
iterator insert( const_iterator hint, node_type&& nh );

将元素插入到容器中,如果容器中还没有包含具有等效键的元素。

  • (1-2) 插入值。

    如果容器中有具有等效键的元素,则在给定范围的上限处插入。重载 (2) 等价于 emplace(std::forward<P>(value)),并且仅在 std::is_constructible<value_type, P&&>::value == true 时才参与重载解析。
  • (3-4) 尽可能地将值插入到紧邻提示的位置。重载 (4) 等价于 emplace_hint(hint, std::forward<P>(value)),并且仅在 std::is_constructible<value_type, P&&>::value == true 时才参与重载解析。

  • (5) 插入范围 [ first, last ) 中的元素。

  • (8) 插入初始化列表 ilist 中的元素。

  • (9) 如果 nh 是一个空的节点句柄,则不执行任何操作。否则,如果容器中尚不包含与 nh.key() 等效键的元素,则将 nh 所拥有的元素插入到容器中。如果 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。

  • (10) 如果 nh 是一个空的节点句柄,则不执行任何操作并返回末尾迭代器。否则,如果容器中尚不包含与 n.key() 等效键的元素,则将 nh 所拥有的元素插入到容器中,并返回指向与 nh.key() 等效键的元素的迭代器(无论插入成功与否)。如果插入成功,则 nh 将被移动;否则,它将保留对元素的拥有权。元素尽可能地插入到紧邻提示的位置。如果 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。

不使任何迭代器或引用失效。

参数

  • 提示
    指向新元素将要插入位置之前的迭代器
  • value - 要插入的元素值
  • first, last - 要插入的元素范围
  • ilist - 要从中插入值的初始化列表
  • nh - 兼容的节点句柄

类型要求

返回值

  • (1-4) 返回指向已插入元素的迭代器。
  • (5-6) (无)
  • (7-8) 如果 nh 为空,则为结束迭代器;否则,为指向插入元素的迭代器。

复杂度

  • (1-2) 与容器大小呈对数关系 - O(log size())
  • (4-6)
    如果插入发生在提示符之前的位置,则为分摊常数 - O(1)
    否则,与容器大小呈对数关系 - O(log size())
  • (7-8) O(N * log(size() + N)),其中 N 是要插入的元素数量。
  • (9) 容器大小的对数 - O(log size())
  • (10)
    如果插入发生在提示符之前的位置,则为分摊常数 - O(1)
    否则为容器大小的对数 - O(log size())

异常

  • (1-4) 如果任何操作抛出异常,插入无效。
重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

示例

Main.cpp
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <functional>
#include <string_view>

template<class M>
void print(const std::string_view rem, const M& mmap)
{
std::cout << rem << " ";
for (const auto & e : mmap)
std::cout << "{" << e.first << "," << e.second << "} ";
std::cout << '\n';
}

int main()
{
// list-initialize
std::multimap<int, std::string, std::greater<int>> mmap
{{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
print("#1", mmap);

// insert using value_type
mmap.insert(decltype(mmap)::value_type(5, "pqr"));
print("#2", mmap);

// insert using pair
mmap.insert(std::pair{6, "uvw"});
print("#3", mmap);

mmap.insert({7, "xyz"});
print("#4", mmap);

// insert using initializer_list
mmap.insert({{5, "one"}, {5, "two"}});
print("#5", mmap);

// insert using a pair of iterators
mmap.clear();
const auto il = { std::pair{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"} };
mmap.insert(il.begin(), il.end());
print("#6", mmap);
}
输出
#1 {5,def} {3,baz} {2,foo} {2,bar} {1,abc}
#2 {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#3 {6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#4 {7,xyz} {6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#5 {7,xyz} {6,uvw} {5,def} {5,pqr} {5,one} {5,two} {3,baz} {2,foo} {2,bar} {1,abc}
#6 {3,ü} {2,ё} {2,ö} {1,ä}
本文档源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”可查看对本文档进行的所有更改。
悬停查看原始许可证。

std::multimap insert() 方法

// (1) Non const version only
iterator insert( value_type&& value );

// (2) Non const version only
template< class P >
iterator insert( P&& value );

// (3) Non const version only
iterator insert( const_iterator hint, value_type&& value );

// (4) Non const version only
template< class P >
iterator insert( const_iterator hint, P&& value );

// (5) Non const version only
template< class InputIt >
void insert( InputIt first, InputIt last );

// (6) Non const version only
void insert( std::initializer_list<value_type> ilist );

// (7) Non const version only
iterator insert( node_type&& nh );

// (8) Non const version only
iterator insert( const_iterator hint, node_type&& nh );

将元素插入到容器中,如果容器中还没有包含具有等效键的元素。

  • (1-2) 插入值。

    如果容器中有具有等效键的元素,则在给定范围的上限处插入。重载 (2) 等价于 emplace(std::forward<P>(value)),并且仅在 std::is_constructible<value_type, P&&>::value == true 时才参与重载解析。
  • (3-4) 尽可能地将值插入到紧邻提示的位置。重载 (4) 等价于 emplace_hint(hint, std::forward<P>(value)),并且仅在 std::is_constructible<value_type, P&&>::value == true 时才参与重载解析。

  • (5) 插入范围 [ first, last ) 中的元素。

  • (8) 插入初始化列表 ilist 中的元素。

  • (9) 如果 nh 是一个空的节点句柄,则不执行任何操作。否则,如果容器中尚不包含与 nh.key() 等效键的元素,则将 nh 所拥有的元素插入到容器中。如果 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。

  • (10) 如果 nh 是一个空的节点句柄,则不执行任何操作并返回末尾迭代器。否则,如果容器中尚不包含与 n.key() 等效键的元素,则将 nh 所拥有的元素插入到容器中,并返回指向与 nh.key() 等效键的元素的迭代器(无论插入成功与否)。如果插入成功,则 nh 将被移动;否则,它将保留对元素的拥有权。元素尽可能地插入到紧邻提示的位置。如果 nh 不为空且 get_allocator() != nh.get_allocator(),则行为未定义。

不使任何迭代器或引用失效。

参数

  • 提示
    指向新元素将要插入位置之前的迭代器
  • value - 要插入的元素值
  • first, last - 要插入的元素范围
  • ilist - 要从中插入值的初始化列表
  • nh - 兼容的节点句柄

类型要求

返回值

  • (1-4) 返回指向已插入元素的迭代器。
  • (5-6) (无)
  • (7-8) 如果 nh 为空,则为结束迭代器;否则,为指向插入元素的迭代器。

复杂度

  • (1-2) 与容器大小呈对数关系 - O(log size())
  • (4-6)
    如果插入发生在提示符之前的位置,则为分摊常数 - O(1)
    否则,与容器大小呈对数关系 - O(log size())
  • (7-8) O(N * log(size() + N)),其中 N 是要插入的元素数量。
  • (9) 容器大小的对数 - O(log size())
  • (10)
    如果插入发生在提示符之前的位置,则为分摊常数 - O(1)
    否则为容器大小的对数 - O(log size())

异常

  • (1-4) 如果任何操作抛出异常,插入无效。
重要

本节需要改进。您可以通过编辑此文档页面来帮助我们。

示例

Main.cpp
#include <iostream>
#include <string>
#include <map>
#include <utility>
#include <functional>
#include <string_view>

template<class M>
void print(const std::string_view rem, const M& mmap)
{
std::cout << rem << " ";
for (const auto & e : mmap)
std::cout << "{" << e.first << "," << e.second << "} ";
std::cout << '\n';
}

int main()
{
// list-initialize
std::multimap<int, std::string, std::greater<int>> mmap
{{2, "foo"}, {2, "bar"}, {3, "baz"}, {1, "abc"}, {5, "def"}};
print("#1", mmap);

// insert using value_type
mmap.insert(decltype(mmap)::value_type(5, "pqr"));
print("#2", mmap);

// insert using pair
mmap.insert(std::pair{6, "uvw"});
print("#3", mmap);

mmap.insert({7, "xyz"});
print("#4", mmap);

// insert using initializer_list
mmap.insert({{5, "one"}, {5, "two"}});
print("#5", mmap);

// insert using a pair of iterators
mmap.clear();
const auto il = { std::pair{1, "ä"}, {2, "ё"}, {2, "ö"}, {3, "ü"} };
mmap.insert(il.begin(), il.end());
print("#6", mmap);
}
输出
#1 {5,def} {3,baz} {2,foo} {2,bar} {1,abc}
#2 {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#3 {6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#4 {7,xyz} {6,uvw} {5,def} {5,pqr} {3,baz} {2,foo} {2,bar} {1,abc}
#5 {7,xyz} {6,uvw} {5,def} {5,pqr} {5,one} {5,two} {3,baz} {2,foo} {2,bar} {1,abc}
#6 {3,ü} {2,ё} {2,ö} {1,ä}
本文档源自此 CppReference 页面。它可能经过修改以进行改进或满足编辑偏好。点击“编辑此页面”可查看对本文档进行的所有更改。
悬停查看原始许可证。