map<string,map<int,vector<int> > >怎么访问里面的map容器
voidinfor::out(map<string,map<int,vector<int>>>result){constchar*RESULT_FILE_NAME="re...
void infor::out(map<string,map<int,vector<int> > >result){
const char *RESULT_FILE_NAME = "result.txt";
ofstream outf(RESULT_FILE_NAME);
for(map<string,map<int,vector<int> > >::iterator it = result.begin();it != result.end();++it){
for(map<int,vector<int> >::iterator it_ = it.begin();it_ != it.end();++it_){//这一行会报错
outf<<it->first<<" - - ";
outf<<" <<新闻"<<it_->first<<">> ";
int j;
for(j=1;j<it_->second.size()-1;j++)
outf<< it_->second[j] <<" ";
outf<< it_->second[j];
outf<<endl;
}
}
}
//\reader.cpp|90|error: 'struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > > > >' has no member named 'begin'|
//\reader.cpp|90|error: 'struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > > > >' has no member named 'end'| 展开
const char *RESULT_FILE_NAME = "result.txt";
ofstream outf(RESULT_FILE_NAME);
for(map<string,map<int,vector<int> > >::iterator it = result.begin();it != result.end();++it){
for(map<int,vector<int> >::iterator it_ = it.begin();it_ != it.end();++it_){//这一行会报错
outf<<it->first<<" - - ";
outf<<" <<新闻"<<it_->first<<">> ";
int j;
for(j=1;j<it_->second.size()-1;j++)
outf<< it_->second[j] <<" ";
outf<< it_->second[j];
outf<<endl;
}
}
}
//\reader.cpp|90|error: 'struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > > > >' has no member named 'begin'|
//\reader.cpp|90|error: 'struct std::_Rb_tree_iterator<std::pair<const std::basic_string<char, std::char_traits<char>, std::allocator<char> >, std::map<int, std::vector<int, std::allocator<int> >, std::less<int>, std::allocator<std::pair<const int, std::vector<int, std::allocator<int> > > > > > >' has no member named 'end'| 展开
1个回答
展开全部
it 为iterator,类似指针,所以访问时候需要->,而不是用".",如果一定要用".",那么使用如下:
*(it).begin()。
*(it).begin()。
更多追问追答
追问
改了之后还是会报错。it->begin()和*(it).begin().都会报错。错误还是原来那个
追答
哦,忘记说清楚了,这里不用直接使用begin,因为*(it)包含两部分内容 (key:int, value: map),你要遍历map那么就需要拿到这个map
使用
it_ = it->second.begin(); it_ != it->second().end();
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询