hdu 2072 单词数(map的简单应用)

偶刚开始学C++……路过别喷,欢迎指出不足~~~~

 

 

#include<iostream> #include<string> #include<map> using namespace std; int main() { map<string,int> word_count; string word; string art; char flag; while(getline(cin,art) && art[0] != '#'){ for(string :: size_type i = 0;i != art.size();i++){ while(art[i] != ' ' ){ word += art[i]; i++; if(i == art.size()){ i--; break; } } if(!word.empty()){ word_count[word] = 1; word.clear(); } } cout <<word_count.size() <<endl; word_count.clear(); } return 0; } 

 

6.2

今天才发现应该是用set做的,再结合getline()与istringstream逐个取词,这样比上面的简单多了

 

代码:

 

#include<iostream> #include<set> #include<string> #include<sstream> using namespace std; int main() { string art; while(getline(cin,art) && art != "#"){ istringstream stream(art); string word; set<string> map; while(stream >>word){ //cout <<word <<" "; map.insert(word); } cout <<map.size() <<endl; } return 0; } 

你可能感兴趣的:(c,String,Stream)