Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/129.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 为什么在下面的示例中std::map::insert失败?_C++_C++11_Insert_C++17_Stdmap - Fatal编程技术网

C++ 为什么在下面的示例中std::map::insert失败?

C++ 为什么在下面的示例中std::map::insert失败?,c++,c++11,insert,c++17,stdmap,C++,C++11,Insert,C++17,Stdmap,多亏了关于Bartlomiej Filipek细节的好书C++17。我发现了一些在std::map上下文中使用insert的示例 所以我访问了cppreference.com,以便更好地了解它的工作原理 但是,在页面底部有一个很有价值的长示例,它为我们提供了使用std::map::insert的不同示例。 我已经在我的计算机上亲自测试了它,试图理解为什么使用重载1和重载4,插入失败 老实说,我不明白为什么 我非常感谢您解释这两个特定示例中发生的情况(插入失败的原因),因为这似乎是一个非常有趣的

多亏了关于Bartlomiej Filipek细节的好书C++17。我发现了一些在
std::map
上下文中使用
insert
的示例

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

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

老实说,我不明白为什么

我非常感谢您解释这两个特定示例中发生的情况(插入失败的原因),因为这似乎是一个非常有趣的示例,同时使用了“结构化绑定”的概念

#包括
#包括
#包括
#包括
使用名称空间std::literals;
模板
作废printInsertionStatus(It、bool成功)
{
std::cout如
std::map::insert
上所述:

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

//重载1
中:

迭代器
it_hinata
指向最后插入的条目,即
{“hinata”s,162.8}
,如果尝试输入相同的键值对,则插入失败,因此
success2==false

//重载4
中:

迭代器
it_hinata
仍然指向相同(首先)插入的键值对(即相同的
{“hinata”s,162.8}
)。因此,在上述情况下,相同的原因是插入失败。这意味着映射的大小(即
KarasunOplayerLights
)插入调用后保持不变,条件
std::size(karasunoplayerhights)!=n
计算为
false

以下是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";
}

此处缺少一些内容。当您使用暗示插入时,需要调用lower_bound以获取迭代器。您能显示完整的代码吗?-似乎缺少一些内容。我为您发送了完整的代码。@D.K.“这似乎是一个非常有趣的示例,同时使用了“结构化绑定”的概念。”请记住,这个问题与这里的结构化绑定无关。@D.K.自从C++17的insert_或_assign()以来,它就有点过时了,但是有一种习惯用法,当您使用lower_bound()后跟键比较来执行与find()等价的操作时。然后,如果找不到,迭代器将用于暗示的插入。
#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