zoukankan      html  css  js  c++  java
  • C++ map, unordered_map

    map

    低层数据结构:红黑树

    基本用法:

    #include <iostream>
    #include <map>
    using namespace std;
    
    int main()
    {
        map<char, int> mymap;
        mymap['a'] = 0; //插入数据
        mymap['b'] = 1;
        mymap['c'] = 2;
        map<char, int>::iterator it;
        //遍历
        for(it = mymap.begin(); it!=mymap.end(); it++)
        {
            cout<<it->first<<" "<<it->second<<endl;
        }
        //查找
        it = mymap.find('c');
        if(it == mymap.end())
            cout<<"Not find"<<endl;
        else
            cout<<it->first<<" "<<it->second<<endl;
        return 0;
    }

    unordered_map

    底层数据结构:哈希表

    #include <iostream>
    #include <unordered_map>
    using namespace std;
    
    int main()
    {
        unordered_map<char, int> mymap;
        mymap['a'] = 0; //插入数据
        mymap['b'] = 1;
        mymap['c'] = 2;
        unordered_map<char, int>::iterator it;
        //遍历
        for(it = mymap.begin(); it!=mymap.end(); it++)
        {
            cout<<it->first<<" "<<it->second<<endl;
        }
        //查找
        it = mymap.find('c');
        if(it == mymap.end())
            cout<<"Not find"<<endl;
        else
            cout<<it->first<<" "<<it->second<<endl;
        return 0;
    }
  • 相关阅读:
    贮油点问题(C++)
    二维数组操作
    二的幂次方(递归)
    [haoi2009]巧克力
    距离最远的牛
    ssh注解basedao简单的实现
    @service中构造方法报错
    spring注解 构造函数问题
    json与gson
    AsyncTask异步类的简单操作
  • 原文地址:https://www.cnblogs.com/xumaomao/p/11349062.html
Copyright © 2011-2022 走看看