繁体   English   中英

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

[英]Inserting std::tuple into the std::map

以下示例代码无法编译,我无法弄清楚如何在地图中插入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插入地图的正确方法是什么

要么做

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));

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM