- #include <iostream>
- #include <sstream>
- #include <string>
- #include <list>
- #include <map>
- #include <vector>
- #include <utility>
- #include <iomanip> //控制格式输出的,
- using namespace std;
- int main(int argc,char **argv)
- {
- string line1="We were her pride of 10 she named us: ";
- string line2="Benjamin, phoenix, the prodigal";
- string line3="and perspicacious pacific suzanne";
- string sentence=line1+' '+line2+' '+line3;
- std::istringstream stream(sentence);
- string word;
- map<string,int> word_count;
- while(stream>>word)
- ++word_count[word];
- int words=0;
- for(map<string,int>::iterator iter=word_count.begin();
- iter!=word_count.end();++iter)
- {
- words+=iter->second;
- }
- cout<<words<<endl;
- return 0;
- }
统计单词数的例子
最新推荐文章于 2022-12-13 23:10:15 发布