以下示例代码无法编译,我不知道如何将 int
和 tuple
插入到 map 中。
#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 的正确方法是什么
最佳答案
要么做
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));
关于c++ - 将 std::tuple 插入 std::map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55401103/