統計一句話中不同的單詞數 sstream 分別用set和map
阿新 • • 發佈:2018-11-16
#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(); }