简体   繁体   English

访问地图的值,它是一个向量对 C++

[英]Accessing a map's value which is a vector pair C++

I have a map declared by我有一张地图

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

As I iterate through the map I want to access the data in the vector to print out and do operations on, I've been trying to use myMap.at(string1).first to access the integer associated with the key string1 , but I keep getting type errors.当我遍历地图时,我想访问向量中的数据以打印出来并对其进行操作,我一直在尝试使用myMap.at(string1).first来访问与键string1关联的整数,但我不断收到类型错误。 Could someone explain the best way to access the data in this vector pair from its key?有人可以解释从其密钥访问此向量对中数据的最佳方法吗?

Given:鉴于:

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

the:这:

myMap.at(string1).first

obviously should not compile.显然不应该编译。 myMap.at() , if successfully matched with an associated value, will give you a vector . myMap.at()如果与关联值成功匹配,则会为您提供一个vector You are trying to use .first on that vector .您正在尝试在该vector上使用.first vector s do not have .first and such members. vector没有.first和这样的成员。 Maybe you instead want:也许你想要:

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

If you are sure that you want your original data type, accessing the first pair associated with string1 would be:如果您确定需要原始数据类型,则访问与string1关联的第一对将是:

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

This will fetch a vector<pair<int, string>> associated with string1 and give you the first element in the first pair of that vector.这将获取与string1关联的vector<pair<int, string>>并为您提供该向量的第一对中的一个元素

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM