gpt4 book ai didi

c++ - 如何将二维 vector double 映射到字符串?

转载 作者:行者123 更新时间:2023-11-30 04:54:56 25 4
gpt4 key购买 nike

我正在尝试将二维 vector 映射到字符串;例如,具体来说,我想将每一行分配给其正确的字符串,例如 1、2、7 为是。

另外,如果我想将不同的键分配给相同的值怎么办?

在第 1 行和第 2 行中有一个值 2,我想将 yes 分配给第 1 行中的 2,并将 no 分配给第 2 行中的 2,我该怎么做呢?

第 1 行:{1, 2, 7} 映射到"is"

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

第 3 行:{5, 7, 8} 映射到"is"

我的二维 vector :

 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

最佳答案

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

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;

如果你想用多个值初始化你的 map ,你需要在一个大括号内提供几个 {key, value}:

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

关于c++ - 如何将二维 vector double 映射到字符串?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53356383/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com