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

4

下面这段示例代码无法编译,我无法弄清楚如何将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中?


如果您的值类型为 int,则会出现相同的错误。您认为应该调用哪个 insert 重载 - 463035818_is_not_a_number
1个回答

10

要么去做

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

我不知道std::make_pair,从你的回答中学到了新东西! - user11157650
1
@zebanovich - std::piecewise_construct 的可能性是我最近几天也学到的东西。 :-) - Benjamin Bihler

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接