[英]How to print vector<map<string, int> > in C++
In vector < map< string, int > >
type.在vector < map< string, int > >
类型中。
When I know the index of vector, I just want to print out the string of map, but it does not work.当我知道vector的索引时,我只想打印出map的字符串,但它不起作用。
Error occurs in错误发生在
cout<< myVec.at(intTmp)->first<<"\n";
I'm not sure how to access to the map inside of the vector.我不确定如何访问矢量内部的地图。
#include <iostream>
#include <string>
#include <map>
#include <vector>
using namespace std;
int N, M;
vector<map<string, int> > myVec;
bool isDigits(string& s){
for(int i=0; i<s.size(); ++i){
if(!isdigit(s[i])) return false;
}
return true;
}
int main()
{
cin >> N >> M;
for(int i=1; i<=N; ++i){
string tmp;
cin.clear();
getline(cin, tmp);
myVec.emplace_back(tmp, i);
}
for(int i=1; i<=M; ++i){
string tmp1;
cin.clear();
getline(cin, tmp1);
if(isDigits(tmp1)){
int intTmp = atoi(tmp1.c_str());
cout<< myVec.at(intTmp)->first<<"\n";
}
}
return 0;
}
Use this snippet:使用这个片段:
for(auto map2:myVec)
{
for(auto it = map2.begin();it != map2.end();it++)
cout << (it->first).c_str() << " " << it->second <<endl;
}
It's a map you have to iterate over the map这是一张你必须遍历地图的地图
vector< map<string,int> > myVec;
for( int i=0; i< myVec.size(); i++) {
for(auto it = myVec[i].begin(); it != myVec[i].end(); it++) {
cout<<it->first;
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.