题意:输入字符串,按字典序输出字符串和它所占的百分比。
map是一个容器,存入数据的时候默认字典序存入,只是不经常用所以很难控制输入输出。
对于getline的解释:
istream&getline ( istream &is , string &str, char delim );里面分别是:输入方式,输入目标,停止标记,来源:http://blog.sina.com.cn/s/blog_60263c1c0101ck25.html
ios::sync_with_stdio(false);<span style="font-family:Courier New;">//传说可以加快cin,cout的速度
来源:http://blog.csdn.net/yujuan_mao/article/details/8119529
</span>
it->first指向string it->second指向int
#include <iostream>
#include <map>
#include <iomanip>
#include <string>
using namespace std;
int main()
{
// ios::sync_with_stdio(false);
map<string, int> a;
string name;
int count = 0;
while (getline(cin, name))
{
++a[name];
count++;
}
for (map<string, int>::const_iterator map_it = a.begin(); map_it != a.end(); ++map_it)
{
cout << map_it->first << ' ' << fixed << setprecision(4) << (map_it->second)*100.0 / count << endl;
}
}