簡體   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