新手遇到地图:insert中的有关问题
新手遇到map::insert中的问题
//cout number of times each world occurs in the input
map<string,int> word_count; //empty map from string to int
string word;
while(cin>>word){
//inserts element with key equal to world and value 1
//if word already in word_cout,insert does nothing
pair<map<string,int>::iterator,bool> ret=
word_cout.insert(make_pair(word,1));
if(!ret.second) //word already in word_cout
++ret.first->second; //increment couter
}
这是一个练习使用insert重写的单词程序,为什么最后的给counter加1不直接用++ret.second?
------解决方案--------------------
pair<map<string,int>::iterator,bool> ret
这一句,ret是什么类型?它的second双是什么类型?
------解决方案--------------------
你自己都知道是这个类型,那ret.second是什么?很明显就是一个bool,对bool做++操作?你开什么玩笑。
看清楚原代码,是先用ret->first,也就是获得了map<string,int>,再指向这个map的second.这才是int.
//cout number of times each world occurs in the input
map<string,int> word_count; //empty map from string to int
string word;
while(cin>>word){
//inserts element with key equal to world and value 1
//if word already in word_cout,insert does nothing
pair<map<string,int>::iterator,bool> ret=
word_cout.insert(make_pair(word,1));
if(!ret.second) //word already in word_cout
++ret.first->second; //increment couter
}
这是一个练习使用insert重写的单词程序,为什么最后的给counter加1不直接用++ret.second?
------解决方案--------------------
pair<map<string,int>::iterator,bool> ret
这一句,ret是什么类型?它的second双是什么类型?
------解决方案--------------------
你自己都知道是这个类型,那ret.second是什么?很明显就是一个bool,对bool做++操作?你开什么玩笑。
看清楚原代码,是先用ret->first,也就是获得了map<string,int>,再指向这个map的second.这才是int.