在STL帮助文档上有如下map::insert重载版本:
pair
iterator insert ( iterator position, const value_type& x );
template
void insert ( InputIterator first, InputIterator last );
你的insert是和第一个版本的参数匹配。参看其中给出的返回值说明:
The first version returns a pair, with its member
pair::first set to an iterator pointing to either the newly inserted
element or to the element that already had its same value in the map. The pair::second element in the pair is set to true if a new element was
inserted or false if an element with the same value existed.
大意是:
返回的pair的first被设置成指向你所插入元素或map中已有的同值(指的是键)元素的迭代器。如果插入元素second成员被设置成true,否则为false.
所以你的代码中返judge的first实际上是指向你插入元素的迭代器。由于map要求键的唯一性,所以只有第一次插入成功,后面的都说是插入失败,返回map中键为word的那个元素的迭代器。也就是judge的第二个参数在后续插入中都为false,所以会进行累加。最后,使用operator[]获取键对应的值是累加两次的值!