跳到主要内容

std::map insert() 方法

// (1) Non const version only
std::pair<iterator, bool> insert( const value_type& value );

// (2) Non const version only
template< class P >
std::pair<iterator, bool> insert( P&& value );

// (3) Non const version only
std::pair<iterator, bool> insert( value_type&& value );

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

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

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

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

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

// (9) Non const version only
insert_return_type insert( node_type&& nh );

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

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

  • (1-3) 插入值。重载 (2) 等同于 emplace(std::forward<P>(value)),并且仅当 std::is_constructible<value_type, P&&>::value == true 时才参与重载决议。

  • (4-6) 尽可能地在接近提示的位置插入值。重载 (5) 等同于 emplace_hint(hint, std::forward<P>(value)),并且仅当 std::is_constructible<value_type, P&&>::value == true 时才参与重载决议。

  • (7) 插入范围 [ first, last ) 中的元素。如果范围中有多个元素具有等效的键,则插入哪个元素是未指定的(等待 LWG2844)。

  • (8) 从初始化列表 ilist 插入元素。如果范围中有多个元素具有等效的键,则插入哪个元素是未指定的(等待 LWG2844)。

  • (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 - 要插入的元素值
  • count - 要插入的元素数量
  • first, last - 要插入的元素范围
  • ilist - 要从中插入值的初始化列表
  • nh - 兼容的节点句柄

类型要求

返回值

  • (1-3) 返回一个对,包含一个指向被插入元素的迭代器(或指向阻止插入的元素的迭代器)和一个表示是否发生插入的 bool 值。
  • (4-6) 返回一个指向被插入元素的迭代器,或指向阻止插入的元素的迭代器。
  • (7-8) (无)
  • (9) 返回一个 insert_return_type,其成员初始化如下:
    • 如果 nh 为空,则 insertedfalsepositionend(),并且 nodeempty
    • 否则,如果插入发生,则 insertedtrueposition 指向已插入元素,nodeempty
    • 如果插入失败,则 insertedfalsenode 具有 nh 的先前值,position 指向一个键与 nh.key() 等效的元素。
  • (10) 如果 nh 为空,则为末尾迭代器;如果插入发生,则为指向被插入元素的迭代器;如果插入失败,则为指向与 nh.key() 等效的键的元素的迭代器。

复杂度

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

异常

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

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

备注

带提示的插入 (4-6) 不返回布尔值,以便与顺序容器(如 std::vector::insert())上的位置插入签名兼容。这使得创建泛型插入器(如 std::inserter())成为可能。检查带提示的插入是否成功的一种方法是比较插入前后的 size()

示例

Main.cpp
#include <iomanip>
#include <iostream>
#include <map>
#include <string>

using namespace std::literals;

template<typename It>
void printInsertionStatus(It it, bool success)
{
std::cout << "Insertion of " << it->first << (success ? " succeeded\n" : " failed\n");
}

int main()
{
std::map<std::string, float> karasunoPlayerHeights;

// Overload 3: insert from rvalue reference
const auto [it_hinata, success] = karasunoPlayerHeights.insert({"Hinata"s, 162.8});
printInsertionStatus(it_hinata, success);

{
// Overload 1: insert from lvalue reference
const auto [it, success2] = karasunoPlayerHeights.insert(*it_hinata);
printInsertionStatus(it, success2);
}
{
// Overload 2: insert via forwarding to emplace
const auto [it, success] = karasunoPlayerHeights.insert(std::pair{"Kageyama", 180.6});
printInsertionStatus(it, success);
}

{
// Overload 6: insert from rvalue reference with positional hint
const std::size_t n = std::size(karasunoPlayerHeights);
const auto it = karasunoPlayerHeights.insert(it_hinata, {"Azumane"s, 184.7});
printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
}
{
// Overload 4: insert from lvalue reference with positional hint
const std::size_t n = std::size(karasunoPlayerHeights);
const auto it = karasunoPlayerHeights.insert(it_hinata, *it_hinata);
printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
}
{
// Overload 5: insert via forwarding to emplace with positional hint
const std::size_t n = std::size(karasunoPlayerHeights);
const auto it = karasunoPlayerHeights.insert(it_hinata, std::pair{"Tsukishima", 188.3});
printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
}

auto node_hinata = karasunoPlayerHeights.extract(it_hinata);
std::map<std::string, float> playerHeights;

// Overload 7: insert from iterator range
playerHeights.insert(std::begin(karasunoPlayerHeights), std::end(karasunoPlayerHeights));

// Overload 8: insert from initializer_list
playerHeights.insert({{"Kozume"s, 169.2}, {"Kuroo", 187.7}});


// Overload 9: insert node
const auto status = playerHeights.insert(std::move(node_hinata));
printInsertionStatus(status.position, status.inserted);

node_hinata = playerHeights.extract(status.position);
{
// Overload 10: insert node with positional hint
const std::size_t n = std::size(playerHeights);
const auto it = playerHeights.insert(std::begin(playerHeights), std::move(node_hinata));
printInsertionStatus(it, std::size(playerHeights) != n);
}


// Print resulting map
std::cout << std::left << '\n';
for (const auto& [name, height] : playerHeights)
std::cout << std::setw(10) << name << " | " << height << "cm\n";
}
输出
Insertion of Hinata succeeded
Insertion of Hinata failed
Insertion of Kageyama succeeded
Insertion of Azumane succeeded
Insertion of Hinata failed
Insertion of Tsukishima succeeded
Insertion of Hinata succeeded
Insertion of Hinata succeeded

Azumane | 184.7cm
Hinata | 162.8cm
Kageyama | 180.6cm
Kozume | 169.2cm
Kuroo | 187.7cm
Tsukishima | 188.3cm
本文源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。

std::map insert() 方法

// (1) Non const version only
std::pair<iterator, bool> insert( const value_type& value );

// (2) Non const version only
template< class P >
std::pair<iterator, bool> insert( P&& value );

// (3) Non const version only
std::pair<iterator, bool> insert( value_type&& value );

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

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

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

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

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

// (9) Non const version only
insert_return_type insert( node_type&& nh );

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

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

  • (1-3) 插入值。重载 (2) 等同于 emplace(std::forward<P>(value)),并且仅当 std::is_constructible<value_type, P&&>::value == true 时才参与重载决议。

  • (4-6) 尽可能地在接近提示的位置插入值。重载 (5) 等同于 emplace_hint(hint, std::forward<P>(value)),并且仅当 std::is_constructible<value_type, P&&>::value == true 时才参与重载决议。

  • (7) 插入范围 [ first, last ) 中的元素。如果范围中有多个元素具有等效的键,则插入哪个元素是未指定的(等待 LWG2844)。

  • (8) 从初始化列表 ilist 插入元素。如果范围中有多个元素具有等效的键,则插入哪个元素是未指定的(等待 LWG2844)。

  • (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 - 要插入的元素值
  • count - 要插入的元素数量
  • first, last - 要插入的元素范围
  • ilist - 要从中插入值的初始化列表
  • nh - 兼容的节点句柄

类型要求

返回值

  • (1-3) 返回一个对,包含一个指向被插入元素的迭代器(或指向阻止插入的元素的迭代器)和一个表示是否发生插入的 bool 值。
  • (4-6) 返回一个指向被插入元素的迭代器,或指向阻止插入的元素的迭代器。
  • (7-8) (无)
  • (9) 返回一个 insert_return_type,其成员初始化如下:
    • 如果 nh 为空,则 insertedfalsepositionend(),并且 nodeempty
    • 否则,如果插入发生,则 insertedtrueposition 指向已插入元素,nodeempty
    • 如果插入失败,则 insertedfalsenode 具有 nh 的先前值,position 指向一个键与 nh.key() 等效的元素。
  • (10) 如果 nh 为空,则为末尾迭代器;如果插入发生,则为指向被插入元素的迭代器;如果插入失败,则为指向与 nh.key() 等效的键的元素的迭代器。

复杂度

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

异常

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

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

备注

带提示的插入 (4-6) 不返回布尔值,以便与顺序容器(如 std::vector::insert())上的位置插入签名兼容。这使得创建泛型插入器(如 std::inserter())成为可能。检查带提示的插入是否成功的一种方法是比较插入前后的 size()

示例

Main.cpp
#include <iomanip>
#include <iostream>
#include <map>
#include <string>

using namespace std::literals;

template<typename It>
void printInsertionStatus(It it, bool success)
{
std::cout << "Insertion of " << it->first << (success ? " succeeded\n" : " failed\n");
}

int main()
{
std::map<std::string, float> karasunoPlayerHeights;

// Overload 3: insert from rvalue reference
const auto [it_hinata, success] = karasunoPlayerHeights.insert({"Hinata"s, 162.8});
printInsertionStatus(it_hinata, success);

{
// Overload 1: insert from lvalue reference
const auto [it, success2] = karasunoPlayerHeights.insert(*it_hinata);
printInsertionStatus(it, success2);
}
{
// Overload 2: insert via forwarding to emplace
const auto [it, success] = karasunoPlayerHeights.insert(std::pair{"Kageyama", 180.6});
printInsertionStatus(it, success);
}

{
// Overload 6: insert from rvalue reference with positional hint
const std::size_t n = std::size(karasunoPlayerHeights);
const auto it = karasunoPlayerHeights.insert(it_hinata, {"Azumane"s, 184.7});
printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
}
{
// Overload 4: insert from lvalue reference with positional hint
const std::size_t n = std::size(karasunoPlayerHeights);
const auto it = karasunoPlayerHeights.insert(it_hinata, *it_hinata);
printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
}
{
// Overload 5: insert via forwarding to emplace with positional hint
const std::size_t n = std::size(karasunoPlayerHeights);
const auto it = karasunoPlayerHeights.insert(it_hinata, std::pair{"Tsukishima", 188.3});
printInsertionStatus(it, std::size(karasunoPlayerHeights) != n);
}

auto node_hinata = karasunoPlayerHeights.extract(it_hinata);
std::map<std::string, float> playerHeights;

// Overload 7: insert from iterator range
playerHeights.insert(std::begin(karasunoPlayerHeights), std::end(karasunoPlayerHeights));

// Overload 8: insert from initializer_list
playerHeights.insert({{"Kozume"s, 169.2}, {"Kuroo", 187.7}});


// Overload 9: insert node
const auto status = playerHeights.insert(std::move(node_hinata));
printInsertionStatus(status.position, status.inserted);

node_hinata = playerHeights.extract(status.position);
{
// Overload 10: insert node with positional hint
const std::size_t n = std::size(playerHeights);
const auto it = playerHeights.insert(std::begin(playerHeights), std::move(node_hinata));
printInsertionStatus(it, std::size(playerHeights) != n);
}


// Print resulting map
std::cout << std::left << '\n';
for (const auto& [name, height] : playerHeights)
std::cout << std::setw(10) << name << " | " << height << "cm\n";
}
输出
Insertion of Hinata succeeded
Insertion of Hinata failed
Insertion of Kageyama succeeded
Insertion of Azumane succeeded
Insertion of Hinata failed
Insertion of Tsukishima succeeded
Insertion of Hinata succeeded
Insertion of Hinata succeeded

Azumane | 184.7cm
Hinata | 162.8cm
Kageyama | 180.6cm
Kozume | 169.2cm
Kuroo | 187.7cm
Tsukishima | 188.3cm
本文源自 此 CppReference 页面。它可能为了改进或编辑者偏好而进行了修改。点击“编辑此页面”查看此文档的所有更改。
悬停查看原始许可证。