STL语法——集合:set 安迪的第一个字典(Andy&a…
2018-06-17 22:28:25来源:未知 阅读 ()
Description
Andy, 8, has a dream - he wants to produce his very own dictionary. This is not an easy task for him, as the number of words that he knows is, well, not quite enough. Instead of thinking up all the words himself, he has a briliant idea. From his bookshelf he would pick one of his favourite story books, from which he would copy out all the distinct words. By arranging the words in alphabetical order, he is done! Of course, it is a really time-consuming job, and this is where a computer program is helpful.
You are asked to write a program that lists all the different words in the input text. In this problem, a word is defined as a consecutive sequence of alphabets, in upper and/or lower case. Words with only one letter are also to be considered. Furthermore, your program must be CaSe InSeNsItIvE. For example, words like "Apple", "apple" or "APPLE" must be considered the same.
Input
The input file is a text with no more than 5000 lines. An input line has at most 200 characters. Input is terminated by EOF.
Output
Your output should give a list of different words that appears in the input text, one in a line. The words should all be in lower case, sorted in alphabetical order. You can be sure that he number of distinct words in the text does not exceed 5000.
Sample Input
Adventures in Disneyland Two blondes were going to Disneyland when they came to a fork in the road. The sign read: "Disneyland Left." So they went home.
Sample Output
a adventures blondes came disneyland fork going home in left read road sign so the they to two went were when
1.中文翻译 |
大体的中文意思就是:输入一个文本,找出所有不同的单词(连续的字母序列),按字典序列从小到大输出。单词不区分大小写,输入输出和上面一样。
2.分析 |
这个题比较简单,我学习的是《算法竞赛入门经典》这本书的代码。
1.学会运用set容器;(有个小知识点,set容器,元素只能出现一次,并且插入可以从小到大排序)
2.学习字符函数库中常用的函数
3.学会stringstream(可参考这篇博文:http://blog.csdn.net/xw20084898/article/details/21939811)
4.最后运行记得是 在空行 ctrl+z +回车。(至于为什么,参考博文:http://blog.csdn.net/kevin_ut/article/details/8576740)
3.说明 |
set是一个集合 和康托前辈的一样 集合中的元素不重复 且集合中的元素是有序的(自动有序化) TY菌介绍说其内部实质是一个平衡树
set不是数组 只能通过迭代器(iterator)把里面的元素倒出来 迭代器相当于是指针 扫描的是地址 因此输出的时候需要用*variation
4.声明 |
需要用到头文件set
set<string> dict; 建立一个集合 其名为dict 基类型是string
5.常用函数 |
dict.begin() dict.end()返回集合最初和最后的元素的地址
这是写这个例题用到的
6.代码 |
1 #include<iostream> 2 #include<string> 3 #include<set> 4 #include<sstream> 5 using namespace std; 6 7 set<string> dict;//set up a set called dict-short for dictionary,and it's based on string type; 8 9 int main(){ 10 string s,buf; 11 while (cin>>s){ 12 for (int i=0;i<s.length();i++) 13 if (isalpha(s[i])) s[i]=tolower(s[i]);//if it's an letter,turn it lowercase.Others turn space and use stringstream to leave it out=ignore it 14 else s[i]=' '; 15 stringstream ss(s); 16 while (ss>>buf) dict.insert(buf);//insert it into the set which is already in order,TYkon said it's a balanced tree inside 17 } 18 for (set<string>::iterator it=dict.begin();it!=dict.end();++it)//iterator just like a point,scan it from beginning to end and output 19 cout<<*it<<endl;//NOTICE output by point 20 return 0; 21 }
还是英文注释不过看懂应该问题不大
isalpha()判断是否是字母 如果是就用头tolower()转换成小写 否则就转为空格 这样是为了字符串流重读入的时候能够把空格去掉
特别强调一下本例中出现的迭代器
1 for (set<string>::iterator it=dict.begin();it!=dict.end();++it)//iterator just like a point,scan it from beginning to end and output 2 cout<<*it<<endl;//NOTICE output by point
迭代器iterator相当于是个类型 it是一个变量 基类型是iterator 它从开始扫到结尾的前一个【据TY菌解释 像数组一样 最后一位是没有值的 数组是一个结束标志 不知集合最后是什么】输出的时候 由于it扫描的是地址 要输出*it
这个好像用到了stl set的语法....
显然对于set我还知之甚少 还需要在今后的学习中不断掌握
今天晚上此时已是0:15,写完博客,明天上课.....
本文部分引用http://blog.csdn.net/ametake
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
- 复习C++语法--string与string_view 2020-05-28
- 复习C++语法--基础篇 2020-05-27
- C++仿函数 2020-05-16
- STL之list 2020-04-30
- C++统一初始化语法(列表初始化) 2020-04-30
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash