将std :: tuple插入到std :: map中

时间:2019-03-28 15:15:07

标签: c++ dictionary insert tuples

以下示例代码无法编译,我无法弄清楚如何在地图中插入inttuple

#include <tuple>
#include <string>
#include <map>

int main()
{
    std::map<int, std::tuple<std::wstring, float, float>> map;
    std::wstring temp = L"sample";

    // ERROR: no instance of overloaded function matches the argument list
    map.insert(1, std::make_tuple(temp, 0.f, 0.f));

    return 0;
}

将示例int, std::tuple插入地图的正确方法是什么

1 个答案:

答案 0 :(得分:5)

要么做

map.insert(std::make_pair(1, std::make_tuple(temp, 0.f, 0.f)));

map.emplace(1, std::make_tuple(temp, 0.f, 0.f));

实际上更好,因为它创建的临时文件更少。

编辑:

甚至有可能根本不创建任何临时工:

map.emplace(std::piecewise_construct, std::forward_as_tuple(1),
    std::forward_as_tuple(temp, 0.f, 0.f));
相关问题