zoukankan      html  css  js  c++  java
  • HDU 1004 MAP【STL__map_的应用】

    强大的MAP,今天终于开始好好学习一次。

    map内部是用红黑树维持的有序结构。

    定义:map<int,string>mapStudent;

    查找的时间复杂度为对数级别.

    1.构造方法学习两种:

    第一种:用insert函数插入pair数据,mapStudent.insert(pair<int, string>(0,"jiangjing"));

    第二种:用数组方式插入数据

    mapStudent[1] = "jiangjing1";  mapStudent[2] =  "jiangjing2";

    2.遍历也学习两种:

    第一种:用迭代器遍历map<int, string>::iterator iter;

    for(iter = mapStudent.begin(); iter != mapStudent.end(); iter++)
    cout<<iter->first<<" " <<iter->second<<endl;

    第二种:用数组遍历

    for(int nIndex = 0; nIndex < nSize; nIndex++)
    cout<<mapStudent[nIndex]<<endl;

    3.用count函数来判定关键字是否出现,出现返回1,没出现返回0;

    用find函数来定位数据出现位置,它返回的一个迭代器,当数据出现时,它返回数据所在位置的迭代器,如果map中没有要查找的数据,它返回的迭代器等于end函数返回的迭代器,程序说明:

    iter = mapStudent.find(1);
    
    if(iter != mapStudent.end())
    cout<<"Find, the value is "<<iter->second<<endl;
    else
    cout<<"Do not Find"<<endl;

    source code:

    //#pragma comment(linker, "/STACK:16777216") //for c++ Compiler
    #include <stdio.h>
    #include <iostream>
    #include <cstring>
    #include <cmath>
    #include <stack>
    #include <queue>
    #include <vector>
    #include <map>
    #include <algorithm>
    #define ll long long
    #define Max(a,b) (((a) > (b)) ? (a) : (b))
    #define Min(a,b) (((a) < (b)) ? (a) : (b))
    #define Abs(x) (((x) > 0) ? (x) : (-(x)))
    using namespace std;
    
    const int INF = 0x3f3f3f3f;
    
    map <string, int> m;
    string a, aa;
    
    int main(){
        int i, j, t, n, numCase = 0;
        while(cin >> n){
            if(0 == n)  break;
            m.clear();
            for(i = 1; i <= n; ++i){
                cin >> a;
                ++m[a];
            }
            map <string, int> ::iterator it;
            int Max = 0;
            for(it = m.begin(); it != m.end(); ++it){
                if(it->second > Max){
                    Max = it->second;
                    aa = it->first;
                }
            }
            cout << aa << endl;
        }
        return 0;
    }
  • 相关阅读:
    starUML学习笔记一
    android 反编译教程
    android asyncTask 笔记
    android v13 的新特性
    ViewPage+Fragment+indicator+Tabhost效果
    android dp sp pt mm in px
    Tabhost+framgent+ViewPager滑动效果
    android studio github 项目导入问题
    Fragment 中 onCreate和onCreateView的区别
    android 抽屉式滑动demo
  • 原文地址:https://www.cnblogs.com/wushuaiyi/p/4089871.html
Copyright © 2011-2022 走看看