gpt4 book ai didi

c++ - 为什么 std::map::insert 在以下示例中失败?

转载 作者:太空狗 更新时间:2023-10-29 21:31:43 25 4
gpt4 key购买 nike

感谢 Bartlomiej Filipek 的好书 C++17 in detail。我正在发现一些使用 insert 的示例在 std::map 的背景下.

所以我访问了 cppreference.com 以更好地了解它的工作原理。

https://en.cppreference.com/w/cpp/container/map/insert

但是,在页面底部,有一个很有值(value)的长示例,它为我们提供了 std::map::insert 的不同用法示例。 .我已经在我的计算机上亲自测试了它,以试图理解为什么使用overload 1overload 4,插入失败。

老实说,我不明白为什么。

如果您能解释这两个具体示例中发生了什么(插入失败的原因),我将不胜感激,因为它似乎是一个非常有趣的示例,它同时使用了“结构化绑定(bind) ”。

#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({ "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, { "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";
}

最佳答案

cppreference.com 所述 std::map::insert:

Inserts element(s) into the container, if the container doesn't already contain an element with an equivalent key.

//重载 1 中:

迭代器 it_hinata 指向最后插入的条目,即 {"Hinata"s, 162.8},如果您尝试输入相同的 key- value 对,插入失败,因此 success2 == false

//重载 4 中:

迭代器 it_hinata 仍然指向相同的(首先)插入的键值对(即相同的 {"Hinata"s, 162.8})。所以,和上面的情况一样,插入失败。这意味着, map 的大小(即 karasunoPlayerHeights)在插入调用后保持不变,条件 std::size(karasunoPlayerHeights) != n 评估为

以下是minimal, complete reproducible example来自 OP 发布的内容:

#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.8f });
printInsertionStatus(it_hinata, success);
{
// Overload 1: insert from lvalue reference
const auto [it, success2] = karasunoPlayerHeights.insert(*it_hinata);
printInsertionStatus(it, success2);
}
{
// 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);
}
// Print resulting map
std::cout << std::left << '\n';
for (const auto& [name, height] : karasunoPlayerHeights)
std::cout << std::setw(10) << name << " | " << height << "cm\n";
}

哪些输出:

Insertion of Hinata succeeded
Insertion of Hinata failed
Insertion of Hinata failed

Hinata | 162.8cm

关于c++ - 为什么 std::map::insert 在以下示例中失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57437363/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com