HDU 2072 单词数

题目:http://acm.hdu.edu.cn/showproblem.php?pid=2072

题意:统计不同的单词个数。

解法:程序中使用set、<sstream>(字符串流)中的istringstream以及string。

有关字符串处理:
使用string 头文件里的 string类型 该类型支持流式读写。
getline()函数可以读一行数据。
有关set:
每个元素只能出现一次的集合。
有关istringstream:
istringstream对象可以绑定一行字符串,然后以空格为分隔符把该行分隔开来。

AC:

#include <iostream>  
#include <cstdio>  
#include <sstream>  
#include <set>  
  
using namespace std;  
  
int main()  
{  
    string s;  
  
    while(getline(cin, s) && s != "#") {  
        istringstream sin(s);  //创建一个字符串流sin,接下来就可以像使用cin一样使用sin。
        set<string> words;  //定义一个set集合,盛放word的不重复个数。
        string w;  
  
        while(sin >> w)  
            words.insert(w);  //向集合words中添加元素w
  
        cout << words.size() << endl;  
    }  
  
    return 0;  
}  
原文地址:https://www.cnblogs.com/zz990728/p/8886294.html