如何将二维向量double映射到字符串?

时间:2018-11-17 23:03:47

标签: c++ dictionary

我正在尝试将2d向量映射到字符串;例如,特别是我想将每一行分配给其正确的字符串,因此将1,2,7分配给yes。

如果我想为相同的值分配不同的键怎么办?

在第1行和第2行中,有一个值2,我想为第1行中的2分配“是”,并为第2行中的2分配“否”?

第1行:{1、2、7}映射为“是”

第2行:{2,3,4}映射为“否”

第3行:{5,7,8}映射为“是”

我的2D向量:

 int N = 3;
 int M = 3;
vector<vector<double>> matrix2d(N, vector<double>(M)); 

我第一行的代码无效:

map < vector<double>, string > map_of_strings;
map_of_strings = {{matrix2d[0][0], matrix2d[0][1], matrix2d[0][2]}, "yes"};

我收到的错误消息是:

1>d:\practice\finalproject\finalproject\source.cpp(118): error C2552: 'map_of_strings' : non-aggregates cannot be initialized with initializer list

1 个答案:

答案 0 :(得分:1)

要分配单个值,您需要按照以下方式进行操作:

map_of_strings [{matrix2d[0][0], matrix2d[1][0], matrix2d[2][0]}] = "yes";

或这种方式:

map_of_strings [{1, 2, 7}] ="yes";

然后,您可以检查给定组合的内容:

cout << map_of_strings [{matrix2d[0][0], matrix2d[1][0], matrix2d[2][0]}] << endl;

如果要使用多个值初始化地图,则需要在大括号内提供几对{key,value}:

map < vector<double>, string > map_of_strings = { {{1, 2, 7},"yes"}, 
                                                  {{2, 3, 4},"no"},
                                                  {{5, 7, 8},"yes"} }; 
相关问题