zoukankan      html  css  js  c++  java
  • 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
    ",cnt->first.c_str(),x);//first指迭代器的第一个元素,即字符串。
            //在迭代器中是string的形式存储,需要用.c_str()来转换成%s,printf不能输出string。必需要转换。
        }
        return 0;
    }
    


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

  • 相关阅读:
    普通锁和分布式锁
    java 正则表达式
    java 字符串转date 格式转换
    消息中间件 kafka
    数据的存储方式:对象存储、文件存储、块存储
    Exceptional Control Flow(6)
    Exceptional Control Flow(5)
    Exceptional Control Flow(4)
    Exceptional Control Flow(3)
    Exceptional Control Flow(2)
  • 原文地址:https://www.cnblogs.com/blfshiye/p/4651202.html
Copyright © 2011-2022 走看看