用来练习库函数的使用,第一段代码练习遍历,第二段练习删除。
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <map> #include <algorithm> using namespace std; map<string, int> Map; map<string, int>::iterator it; char str[1010]; void init() { Map.clear(); } int main() { while(gets(str) && strcmp(str, "#")) { init(); char *p = strtok(str, " "); while(p != NULL) { Map[p]++; p = strtok(NULL, " "); } int count = 0; for(it = Map.begin(); it != Map.end(); it++) if(it->second) { count++; } printf("%d\n", count); } return 0; }
#include <iostream> #include <cstdlib> #include <cstring> #include <cstdio> #include <map> #include <algorithm> using namespace std; map<string, int> Map; map<string, int>::iterator it; char str[1010]; void init() { Map.clear(); } int main() { while(gets(str) && strcmp(str, "#")) { init(); char *p = strtok(str, " "); while(p != NULL) { Map[p]++; p = strtok(NULL, " "); } int count = 0; for(it = Map.begin(); it != Map.end(); it++) if(it->first == "a") { Map.erase(it); } for(it = Map.begin(); it != Map.end(); it++) cout<<it->first<<endl; } return 0; }