UVa笔记

UVa 10815 Andy’s dictionary

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

using namespace std;

set<string>dict;//set is template need to give it a type

int main()
{
	string s, buf;
	while (cin >> s)
	{
		for (int i = 0; i < s.length(); i++)
			if (isalpha(s[i]))
				s[i] = tolower(s[i]);
			else s[i] = ' ';//if not alpha make it whitespace
		stringstream ss(s); //constructor
		while (ss >> buf) dict.insert(buf);
	}
	for (set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
		cout << *it << "\n";
	return 0;
}

Note:

  1. set
    set 属于STL,是一个集合(each element has unique value and cannot be modified)
    使用set时,重复输入的数据只会存一次

  2. stringstream
    可以理解为一个class,use to treat string as stream 我感觉stringstream ss(s); 类似一个拷贝构造函数。将括号内的s复制粘贴到ss中,为啥非再弄个新的类来存储s?
    先说cin读取string,会读取空格,只会在回车符之后完结一次读取,这就是输入流吗。while (cin >> s),利用if the input is successful, cin will yield true and vice versa.读取未知的输入。
    **NOTE:cin返回false时也可以是你输入的和接受类型不匹配。**比如说我一个int变量,你输入了char,会自动退出读取过程。但是string的话,字符数字都能读进去。

while (cin >> s)
	{
		for (int i = 0; i < s.length(); i++)
			if (isalpha(s[i]))
				s[i] = tolower(s[i]);
			else s[i] = ' ';//if not alpha make it whitespace

	 dict.insert(s);
	}
	for (set<string>::iterator it = dict.begin(); it != dict.end(); ++it)
		cout << *it << "\n";
	return 0;

但是,当我没有用stringstream ss(s)甚至不用buf,仍然运行成功而且效果和之前是一样的。
so,为啥要这个东西?

  1. 最后再说iterator,STL里面类似指针的东西,注意使用前声明作用域,如果细说,iterator可以分为五个类,有用于output,input,还有random等等,但是。。感觉没必要背概念,懒得总结(摘抄)了

SIDENOTE:这个程序结束输入需要ctrl+z+enter,然而我还是狂敲键盘好久才停下来它,很迷

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值