POJ 2418 Hardwood Species(STL在map应用)

职务地址: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;
}


版权声明:本文博客原创文章。博客,未经同意,不得转载。

posted @ 2015-07-16 14:20  blfshiye  阅读(108)  评论(0编辑  收藏  举报