- #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;
- }
统计单词数的例子
最新推荐文章于 2024-12-23 14:40:40 发布
本文介绍了一个使用C++编写的简单程序,该程序能够读取一个由多个字符串组成的句子,并统计其中的不同单词数量。通过istringstream类从字符串中逐个读取单词并利用map结构进行计数。
1672

被折叠的 条评论
为什么被折叠?



