使用 std::pair 值放入 std::unordered_map 中
emplace into `std::unordered_map` with `std::pair` value
我正在尝试将值放入 std::unordered
映射中,如下所示:
std::unordered_map<std::string, std::pair<std::string, std::string>> testmap;
testmap.emplace("a", "b", "c"));
这是行不通的,因为:
error C2661: 'std::pair::pair' : no overloaded function takes 3 arguments
我查看了 and this answer,似乎我需要将 std::piecewise_construct
合并到炮位中才能使其正常工作,但我不认为我很清楚该放在哪里在这种情况下。尝试
testmap.emplace(std::piecewise_construct, "a", std::piecewise_construct, "b", "c"); // fails
testmap.emplace(std::piecewise_construct, "a", "b", "c"); // fails
testmap.emplace(std::piecewise_construct, "a", std::pair<std::string, std::string>( std::piecewise_construct, "b", "c")); // fails
有什么方法可以让这些值达到 emplace
?
我正在使用 msvc2013 进行编译以防万一。
您需要使用 std::piecewise_construct
和 std::forward_as_tuple
作为参数。
以下编译:
#include <unordered_map>
int main()
{
std::unordered_map<std::string,std::pair<std::string,std::string>> testmap;
testmap.emplace(std::piecewise_construct,std::forward_as_tuple("a"),std::forward_as_tuple("b","c"));
return 0;
}
我正在尝试将值放入 std::unordered
映射中,如下所示:
std::unordered_map<std::string, std::pair<std::string, std::string>> testmap;
testmap.emplace("a", "b", "c"));
这是行不通的,因为:
error C2661: 'std::pair::pair' : no overloaded function takes 3 arguments
我查看了 std::piecewise_construct
合并到炮位中才能使其正常工作,但我不认为我很清楚该放在哪里在这种情况下。尝试
testmap.emplace(std::piecewise_construct, "a", std::piecewise_construct, "b", "c"); // fails
testmap.emplace(std::piecewise_construct, "a", "b", "c"); // fails
testmap.emplace(std::piecewise_construct, "a", std::pair<std::string, std::string>( std::piecewise_construct, "b", "c")); // fails
有什么方法可以让这些值达到 emplace
?
我正在使用 msvc2013 进行编译以防万一。
您需要使用 std::piecewise_construct
和 std::forward_as_tuple
作为参数。
以下编译:
#include <unordered_map>
int main()
{
std::unordered_map<std::string,std::pair<std::string,std::string>> testmap;
testmap.emplace(std::piecewise_construct,std::forward_as_tuple("a"),std::forward_as_tuple("b","c"));
return 0;
}