(使用STL自带的排序功能7.1.1)POJ 2418 Hardwood Species(map的定义、访问、迭代)
/* * POJ_2418.cpp * * Created on: 2013年10月31日 * Author: Administrator */#include <iostream>#include <cstdio>#include <map>//map默认按字典序进行排序#include <string>using namespace std;int main(){map<string,int> mp;//map的定义string str;int cnt = 0;while(getline(cin,str)){//使用getline()记得要#include<string>cnt++;mp[str]++;//map的使用}//map的迭代for(map<string,int>::iterator it = mp.begin() ; it != mp.end() ;++it){string name = (*it).first;int k = (*it).second;printf("%s %.4lf\n",name.c_str(),double(k)/cnt*100);//c++风格的字符串输出时要.c_str()转化一下}return 0;}