如何从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,std::string>(test2,"string");
   return test_1;
}

错误

'return': 不能从 'std::mapstd::tuple,std::allocator<:pair std::tuple>,std::string>>>' 到 'int'

解决方法

您想返回 std::map<std::tuple<int,int>,std::string>,但您已将返回类型写为 int

#include <map>
#include <tuple>

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