uva-10815-Andy's First Dictionary-stl-set

来源:互联网 发布:中文相似度匹配算法 编辑:程序博客网 时间:2024/05/15 14:34

题目传送门:https://vjudge.net/problem/UVA-10815

主要是一些新的懂东西,(对于我一个开始学c的人来说)。


不管什么,先上完整代码,再一一解释。


#include <iostream>#include <set>#include <sstream>#include  <string>using namespace std;set<string> dit;int main(){    string s,buf;    while(cin>>s)    {        int len=s.length();        for(int i=0; i<len; i++)        {            if(isalpha(s[i]))                s[i]=tolower(s[i]);            else                s[i]=' ';        }        stringstream ss(s);        while(ss>>buf)            dit.insert(buf);    }    for(set<string>::iterator it=dit.begin(); it!=dit.end(); ++it)    {        cout<<*it<<endl;    }    return 0;}



一、set

set关联式容器。set作为一个容器也是用来存储同一数据类型的数据类型,并且能从一个数据集合中取出数据,在set中每个元素的值都唯一,而且系统能根据元素的值自动进行排序。
其实就是有一个名为set的集合,它具有集合唯一性的特性(就是不会出现重复的),并且它已经排序好了(只能是从小到大)。


二、string

        就是一个类似int的新的字符串类型,跟int的用法相似,具体去百度吧!



三、


stringstream ss(s);
        while(ss>>buf)
            dit.insert(buf);

   这三行是完成将s创建副本ss,然后将ss输入到buf中(会过滤空格什么的),然后插入到dit里。


为了好理解一点,我找了一个例子:

stringstream通常是用来做数据转换的。

相比c库的转换,它更加安全,自动和直接。

 

例子一:基本数据类型转换例子 int转string


#include <string>
#include <sstream>
#include <iostream>

int main()
{
    std::stringstream stream;
    std::string result;
    int i = 1000;
    stream << i; //将int输入流
    stream >> result; //从stream中抽取前面插入的int值
    std::cout << result << std::endl; // print the string "1000"

*/


stringstream通常是用来做数据转换的。

相比c库的转换,它更加安全,自动和直接。

 

例子一:基本数据类型转换例子 int转string


#include <string>
#include <sstream>
#include <iostream>

int main()
{
    std::stringstream stream;
    std::string result;
    int i = 1000;
    stream << i; //将int输入流
    stream >> result; //从stream中抽取前面插入的int值
    std::cout << result << std::endl; // print the string "1000"

*/


四、

for(set<string>::iterator it=dit.begin(); it!=dit.end(); ++it)
{
cout<<*it<<endl;
}

这用了那个迭代器,其实就是类似于指针,取一下开始时候的然后判断一下结尾输出。

五、

判断字母
isalpha   判断是否为字母 ,若是这返回非0,反之为0.
isupper   判断是否为大写字母 ,若是这返回非0,反之为0.
islower   判断是否为小写字母 ,若是这返回非0,反之为0.


阅读全文
0 0