感谢 Bartlomiej Filipek 的好书 C ++ 17 。我发现一些在insert
上下文中使用std::map
的示例。
因此,我访问了 cppreference.com ,以更好地了解其工作原理。
https://en.cppreference.com/w/cpp/container/map/insert
但是,在页面底部,有一个有价值且冗长的示例,为我们提供了std::map::insert
用法的不同示例。
我已经在计算机上亲自对其进行了测试,以尝试理解为什么使用重载1 和重载4 插入失败。
老实说,我不明白为什么。
我非常感谢您解释在这两个特定示例中发生的情况(为什么插入失败),因为这似乎是一个非常有趣的示例,它同时使用了“ 结构化绑定”的概念”。
#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";
}
答案 0 :(得分:2)
如 cppreference.com std::map::insert
所述:
将元素插入容器,如果容器不插入 已包含具有等效键的元素。
在// Overload 1
中:
迭代器it_hinata
指向最后插入的条目{"Hinata"s, 162.8}
,如果您尝试输入相同的键值对,则插入失败,因此{ {1}}。
在success2 == false
中:
迭代器// Overload 4
仍指向相同(首先)插入的键值对(即,相同的it_hinata
)。因此,与上述情况相同的原因导致插入失败。也就是说,插入调用后,映射的大小(即{"Hinata"s, 162.8}
)保持不变,条件karasunoPlayerHeights
的求值结果为std::size(karasunoPlayerHeights) != n
。
以下是OP发布的内容minimal, complete reproducible example:
false
输出:
#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";
}