gpt4 book ai didi

c++ - 访问作为 vector 对 C++ 的 map 值

转载 作者:搜寻专家 更新时间:2023-10-31 02:05:04 25 4
gpt4 key购买 nike

我有一个声明的 map

map <string, vector<pair<int, string>>>;

当我遍历 map 时,我想访问 vector 中的数据以打印出来并对其进行操作,我一直在尝试使用 myMap.at(string1).first 来访问与键 string1 关联的整数,但我不断收到类型错误。有人可以解释从其 key 访问此 vector 对中数据的最佳方法吗?

最佳答案

给定:

map <string, vector<pair<int, string>>> myMap;

:

myMap.at(string1).first

显然不应该编译。 myMap.at() , 如果与关联值成功匹配,将为您提供 vector .您正在尝试使用 .first在那vector . vector我们没有 .first和这样的成员。也许您反而想要:

map <string, pair<int, string>> myMap;

如果您确定需要原始数据类型,请访问与 string1 关联的第一对会是:

myMap.at(string1).at(0).first

这将获取 vector<pair<int, string>>string1 有关并为您提供该 vector 的第一对中的第一个元素

关于c++ - 访问作为 vector 对 C++ 的 map 值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52517963/

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