您的位置:首页 > 其它

POJ 2418 Hardwood Species(STL在map应用)

2015-07-16 14:20 489 查看
职务地址:POJ 2418

通过这个题查了大量资料。。知道了非常多曾经不知道的东西。



。。

在代码中凝视说明吧。

代码例如以下:

#include <iostream>
#include <cstdio>
#include <string>
#include <cstring>
#include <stdlib.h>
#include <math.h>
#include <ctype.h>
#include <queue>
#include <map>
#include<algorithm>

using namespace std;
int main()
{
map<string,int>mp;//声明一个map容器,是一种对象
map<string,int>::iterator cnt;//声明一个map迭代器,是一种指针
int sum=0;
char s[40];
while(gets(s))
{
sum++;
mp[s]++;
}
for(cnt=mp.begin();cnt!=mp.end();cnt++)//系统默认排序是按从小到大的顺序,这句代码的意思是用迭代器(即指针0在map中从头到尾遍历
{
double x;
x=cnt->second*100.0/sum;//second指迭代器的第二个元素,即int型的数字
printf("%s %.4lf\n",cnt->first.c_str(),x);//first指迭代器的第一个元素,即字符串。
//在迭代器中是string的形式存储,需要用.c_str()来转换成%s,printf不能输出string。必需要转换。
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: