stringstream切割字符串

stringstream 是将字符串变成字符串迭代器一样,将字符串流在依次拿出,它不会将空格作为流。这样就实现了字符串的空格切割。

#include<bits/stdc++.h>
#include<sstream> //头文件
using namespace std;
int main(){
	string str="nice to meet you";
	stringstream stream(str);
	string s;
	while(stream>>s){
		cout<<s<<endl;
	}
	return 0;
}

输出:

nice
to
meet
you

HDU 2072 单词数

#include<bits/stdc++.h>
using namespace std;
int main() {
	string str1,str2;
	while(getline(cin,str1)) {
		if(str1 == "#")
			break;
		stringstream stream(str1);
		set<string> tp;
		while(stream>>str2) { 
			tp.insert(str2); 
		}
		cout<<tp.size()<<endl; 
	}
	return 0;
}

猜你喜欢

转载自blog.csdn.net/TDD_Master/article/details/86665938