统计一句话中不同的单词数 sstream 分别用set和map

#include <iostream>
#include <stdio.h>
#include<set>
#include<sstream>
#include<string>
#include<map>
using namespace std;

void use_map() {
	string s;
	while (1) {
		map<string, int> strset;
		getline(cin, s);
		if (s[0] == '#')return;
		stringstream ss;
		ss.str(s);
		while (ss >> s) {
			strset[s];
		}
		cout << strset.size() << endl;
	}
}
void use_set(){
	string s;
	while (1) {
		set<string> strset;
		getline(cin, s);
		if (s[0] == '#')break;
		stringstream ss;
		ss.str(s);
		while (ss >> s) {
			strset.insert(s);
		}
		cout << strset.size() << endl;
	}
}
int main()
{
	use_map();
	use_set();

}

猜你喜欢

转载自blog.csdn.net/hanker99/article/details/84113063
今日推荐