簡體   English   中英

如何從cpp中的function返回字典

[英]how to return a dictionary from a function in cpp

我有這個代碼

#include <map>
#include <tuple>

int test(int x, int y){
   std::map<std::tuple<int, int>, std::string> test_1;
   std::tuple<int, int> test2;
   test2 = make_tuple(x,y);
   test_1.insert<std::pair<std::tuple<int, int>, std::string>(test2, "string");
   return test_1;
}

錯誤:

'return': 無法從 'std::mapstd::tuple<int,int,std::string,std::lessstd::tuple<int,int>,std::allocator<std::pair<const std ::tuple<int,int>,std::string>>>' 到 'int'

您想返回std::map<std::tuple<int, int>, std::string>但您已將返回類型寫為int

#include <map>
#include <tuple>

std::map<std::tuple<int, int>, std::string> test(int x, int y){
   std::map<std::tuple<int, int>, std::string> test_1;
   std::tuple<int, int> test2;
   test2 = make_tuple(x,y);
   test_1.insert<std::pair<std::tuple<int, int>, std::string>(test2, "string");
   return test_1;
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM