这是单词统计的迭代器版本: /* ** Copyright (C) QPSOFT.COM All rights reserved. */ #include<iostream> #include<map> #include<utility> #include<string> int main() { using namespace std; map<string, int> word_count; string word; //读入单词并统计其出现次数 cout << "Enter some words:" << endl; while(cin >> word) { //插入元素<word,1> pair<map<string, int>::iterator, bool> ret = word_count.insert(make_pair(word, 1)); if(!ret.second) ++ret.first->second;//单词出现次数加1 } //输出结果 cout << "word/t/t" << "times" << endl; for(map<string, int>::iterator iter = word_count.begin(); iter != word_count.end(); ++iter) { cout << (*iter).first << "/t/t" << (*iter).second << endl; } return 0; }