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

题目传送门: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.


  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
STL是指标准模板库(Standard Template Library),它是C++语言的一部分,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的目标是提供高效、可重用和可扩展的组件,以便开发人员能够更轻松地编写高质量的代码。STL包含了许多常见的数据结构,如vector、list、set、map等,以及各种算法,比如排序、查找、遍历等。通过使用STL,开发人员可以更加高效地处理各种数据结构和算法的问题,提高代码的开发效率和质量。 在STL中,我们可以使用各种容器来存储和管理数据。例如,我们可以使用std::map来创建一个键值对的映射,其中每个键都有一个与之相关联的值。下面是一个示例代码,展示了如何创建和使用一个std::map对象: std::map<std::string, int> disMap() { std::map<std::string, int> tempMap{ {"C语言教程",10},{"STL教程",20} }; return tempMap; } std::map<std::string, int> newMap(disMap()); 在这个示例中,disMap()函数创建了一个临时的std::map对象,并初始化了其中的一些键值对。然后,使用移动构造函数将这个临时对象移动到了一个新的std::map对象newMap中。最终,我们可以通过newMap对象来访问和操作这些键值对。 综上所述,STL是C++中的标准模板库,提供了一系列的模板类和函数,用于支持通用的数据结构和算法。STL的使用可以提高代码的开发效率和质量,并且通过各种容器和算法,可以方便地处理各种数据结构和算法的问题。<span class="em">1</span><span class="em">2</span><span class="em">3</span> #### 引用[.reference_title] - *1* [C++ STL详解超全总结(快速入门STL)](https://blog.csdn.net/qq_50285142/article/details/114026148)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] - *2* *3* [【C++实验】阅读STL源码并分析](https://blog.csdn.net/qq_35760825/article/details/125311509)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v93^chatsearchT3_2"}}] [.reference_item style="max-width: 50%"] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值