#include <stdio.h> #include <iostream> #include <set> #include <string> #include <sstream> #include <algorithm> using namespace std; int main() { string str = "HELLO my name is leo LEE I want to talk ABOUT my name to you";//给定的一个字符串 set<string> set1;//集合容器 istringstream iss(str);//string类的输入流,并往流中添加str的内容 string str2; string tempstr; while (iss >> str2)//str2从iss流获取输入,类似于cin>>str,str从cin输入流获取字符串 { transform(str2.begin(), str2.end(), back_inserter(tempstr), ::tolower);//将str2从头到尾的元素进行转换成小写后,插入到迭代器所指的位置 set1.insert(tempstr); //back_inserter是指向一个对象的末端,形成的功能相当于push.back() tempstr.clear();//清除tempstr的内容 } for (set<string>::iterator p = set1.begin(); p != set1.end(); p++)//使用正向迭代器,从头到尾正向移动 { cout << *p << endl; } return 0;
}
此程序不是最好的解法,只是为了练习c++的STL,同时一定要使用纯c++