题目传送门:https://vjudge.net/problem/UVA-10815
#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
二、string
三、
while(ss>>buf)
dit.insert(buf);
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"
}
*/
四、
{
cout<<*it<<endl;
}

本文介绍了一段C++代码的实现细节,包括如何使用`set`容器来存储不重复的字符串并自动排序,如何利用`string`和`stringstream`进行字符串操作与转换。适用于初学者了解C++中的字符串处理技巧。
509

被折叠的 条评论
为什么被折叠?



