int main()
{
}
看一下你调用这个map类insert方法的原型。
pair <iterator, bool> insert(
);
returns a pair whose bool component returns true if an insertion was made and false if the map already contained an element whose key had an equivalent value in the ordering, and whose iterator component returns the address where a new element was inserted or where the element was already located.
也就是说,如果map中不含有_Val中的key就返回指向这个新插入节点的迭代器和true组成的pair。如果map中含有_Val中的key就返回指向这个已经存在的节点和false组成的pair。
首先你明白这个程序是统计你输入不同的单词个数的。
pair<map<string, int>::iterator, bool> ret = word_count.insert(make_pair(word,1));
这句的意思是把word和1变成pair类型,插入到word_count中。如果map中的key不含word这个字符串,就插入,并且初始化为1,因为是第一次出现,返回指向该节点的迭代器和true组成的pair。如果map中含有,就不做任何操作,返回这个已经存在的节点的迭代器和false组成的pair。
if(!ret.second ){
}
这个判断就是如果插入不成功,说明map中已经含有该单词了,但是insert处没做任何操作,在这里让统计数加1。ret.first是指向该单词key的迭代器。