map的基本操作和使用

  • map 是一种关联容器, 将键值和映射值组合成元素并且按照特定顺序进行排列, 通常键值用来排
    序和唯一标识元素, 映射值用来存储和键值相关的内容;
  • 键值类型和映射值类型不要求一样, 但是通常声明为typedef pair
#include<iostream>
#include <map>
#include <string>

using namespace std;

template <class Key,
          class T,
          class Compare = less<Key>,
          class Alloc = allocator<pair<const, Key, T>>
          > class map;

bool fncomp(char lns, char rns)
{
    return lns < rns;
}

struct clascomp
{
    bool operator()(const char& lns, const char& rns)const
    {
        return lns < rns;

    }
};


int main()
{
    //constructors 的几种构建方式:
    map<char, int> first;
    first['a'] = 10;
    first['b'] = 20;
    first['c'] = 30;
    first['d'] = 40;

    map<char, int> second(first.begin(), first.end());
    map<char, int> third(second);
    map<char, int, clascomp> fourth;

    bool(*fn_pt)(char, char) = fncomp;
    map<char, int, bool(*)(char, char)> fifth(fn_pt);

    //.at()用于返回对键k标识元素的映射值的引用,如果K在容器map里面不存在任何映射值,使
    //用out_of_range抛出异常;相关函数的声明包括:mapped_type& at(const key_type& k);
    //const mapped_type& at(const key_type& k)const;

    map<string, int> mymap =
    {
        {"alpah", 0},
        {"beta", 0},
        {"gamma", 0}
    };
    mymap.at("alpah") = 10;
    mymap.at("beta") = 20;
    mymap.at("gamma") = 30;

    cout << "mymap contains: ";
    for (auto X : mymap)
        cout << "(" << X.first << "," << X.second << ")" << " ";
    cout << endl;

    //.begin(),.end()用于访问里面元素的迭代器

    cout << "mymap contains: ";
    for (map<string, int>::iterator It = mymap.begin(); It != mymap.end(); ++It)
        cout << "(" << It->first + "lixun" << " => " << It->second + 10 << ")" << " ";
    cout << endl;

    //.cbegin().cend() 表示不允许进行修改成员的值
    cout << "mymap contains const: ";
    for (auto It = mymap.cbegin(); It != mymap.cend(); ++It)
        cout << "(" << It->first  << " => " << It->second << ")" << " ";
    cout << endl;

    //.rbegin()以及.rend()是用来进行反向遍历的迭代器;

    cout << "mymap contains reverse: ";
    for (auto It = mymap.rbegin(); It != mymap.rend(); ++It)
        cout << "(" << It->first + "lixun" << " => " << It->second + 10 << ")" << " ";
    cout << endl;
    //.rcbegin()以及.rcend()是用来进行反向遍历的迭代器;

    cout << "mymap contains reverse: ";
    for (auto It = mymap.crbegin(); It != mymap.crend(); ++It)
        cout << "(" << It->first << " => " << It->second << ")" << " ";
    cout << endl;

    //.clear()用于清空里面的元素,函数声明是:void clear() noexcept;

    //.cout()在map里面查找一个和K相等的值,并且返回相等值的数量;

    map<char, int> mymap1;
    mymap1['a'] = 10;
    mymap1['b'] = 9;
    mymap1['c'] = 8;

    char c;
    for ( c = 'a'; c < 'g'; ++c)
    {
        cout << c << " ";
        if (mymap1.count(c) > 0)
            cout << "is an elememt of mymap1;" << endl;
        else
            cout << "is not an element of mymap1;" << endl;
    }

    //.emplace():template<class... Args) pair<iteraotr,bool> emplace(Arg&&...args);
    //如果插入的值是不唯一的,那么就插入一个新的元素,参数的类型和pair的类型是一致的;
    //如果插入成功,那么size就会增长,map里面的元素是按照对象指定的标准进行排序
    //如果元素成功插入,那么就会返回一对迭代器插入元素后的新的位置;
    //如果插入失败,那么就返回在map里面找到的和新插入元素相等的值的位置;

    map<char, int> mymap2;
    mymap2.emplace('x', 1);
    mymap2.emplace('y', 2);
    mymap2.emplace('z', 3);
    mymap2.emplace('k', 4);

    cout << "mymap2 contains: ";
    for (auto &X : mymap2)
        cout << "(" << X.first << ":" << X.second << ")" << " ";
    cout << endl;

    //.emplace_hint():再进行元素插入时可以指定一个位置进行元素的插入,对于元素插入的要求和
    //.emplace()是一样的;

    {
        auto It = mymap2.end();
        It = mymap2.emplace_hint(It, 'b', 11);
        mymap2.emplace_hint(It, 'c', 12);
        It = mymap2.begin();
        mymap2.emplace_hint(It, '3', 19);
        cout << "mymap2.emplace_hint contains: ";
        for (auto X : mymap2)
            cout << "(" << X.first << ":" << X.second << ")" << " ";
        cout << endl;
    }

    //.empty()用于判断是否为空操作:bool empty()const noexcept;


    //.equal_range() pair<const_iterator,const_iterator> equal_range(const key_type& k)const;
    //pair<iterator,iterator> equal_range(const key_type& k);这些函数的作用是用来返回等价于
    //元素K的范围的一个边界;因为map的特性,所以返回的元素只有一个,范围也只有一个;如果没有匹配
    //到元素,那么就返回两个都指向第一个元素的迭代器,并且返回长度是0;
    {
        auto ret = mymap2.equal_range('b');
        cout << "lower bound points to: ";
        cout << ret.first->first << "=>" << ret.first->second << " ";
        cout << endl;
        cout << "upper bound points to: ";
        cout << ret.second->first << "=>" << ret.second->second << " ";
        cout << endl;
    }
    //.erase(): iterator erase(const_iterator position);
    //size_type erase(const key_type& k);
    //iterator erase(const_iterator first,const_iterator last);
    //用于删除单个或者是某个范围里面的元素;
    {
        cout << "mymap2.erase() contains: ";
        auto It = mymap2.find('b');
        mymap2.erase(It);
        mymap2.erase('x');
        It = mymap2.find('k');
        mymap2.erase(It,mymap2.end());
        for(auto It=mymap2.begin();It!=mymap2.end();++It)
            cout << It->first << "=>" << It->second << " ";
        cout << endl;
    }

    //.find()用于查找某一个元素的位置,一共有两个版本:iterator find(const key_type& k);
    //const_iterator find(const key_type& k)const;
    //查找某一个元素的值,如果找不到,就返回最后一个元素的位置;
    {
        mymap2['s']=12;
        mymap2['v']=11;
        mymap2['l']=10;
        mymap2['j']=9;
        mymap2['t']=0;

        auto It = mymap2.find('j');
        if(It!=mymap2.end())
            mymap2.erase(It);
        cout << "mymap2.erase() contains: ";
        cout << "s=>" << mymap2.find('s')->second << " ";
        cout << "l=>" << mymap2.find('l')->second << " ";
        cout << "j=>" << mymap2.find('j')->second << " ";
        cout << "t=>" << mymap2.find('t')->second << endl;
    }

    //.get_allocator()函数声明是:allocator_type get_allocator()const noexcept;
    //用于返回和map关联对象的副本;

    {
        pair<const char,int> *p;
        p=mymap2.get_allocator().allocate(5);
        auto psize = sizeof(map<char,int>::value_type)*5;
        cout << "The allocated array has sizeof " << psize << " bytes" << endl;
        mymap2.get_allocator().deallocate(p,5);
    }

    //.insert()包含的函数声明:
    //pair<iterator,bool> insert(const value_type& val);
    //template<class P> pair(iterator,bool) insert(P&& val);
    //
    //iterator insert(const_iterator position,const value_type& val);
    //template<class P> iterator insert(const_iterator position,P&& val);
    //
    //template<class InputIterator>
    //void insert(InputIterator first,InputIterator last);
    //void insert(initializer_list<value_type> il);
    //再进行元素的插入过程中,元素键是唯一的,所以在进行元素插入时,需要进行检查,
    //如果元素重复,就不进行插入,迭代器返回现有元素,否则进行元素插入;
    //
    {
        map<char,int> mynewmap;
        mynewmap.insert(pair<char,int>('a',101));
        mynewmap.insert(pair<char,int>('b',102));
        mynewmap.insert(pair<char,int>('c',103));
        mynewmap.insert(pair<char,int>('c',104));
        mynewmap.insert(pair<char,int>('d',105));

        pair<map<char,int>::iterator,bool> ret;
        mynewmap.insert(pair<char,int>('b',212));

        if(ret.second==false){
            cout << "element 'b' has already exist " << endl;
        }else {
            cout << "elemen 'b' inseret successful " << endl;
        }
        map<char,int>::iterator it = mynewmap.begin();
        mynewmap.insert(it,pair<char,int>('b',300));
        mynewmap.insert(it,pair<char,int>('c',200));

        map<char,int> anothermap;
        anothermap.insert(mynewmap.begin(),mynewmap.find('c'));

        cout << "mynewmap contains: ";
        for(auto X:mynewmap)
            cout << X.first << " " << X.second << " ";
        cout << endl;
        for(auto X:anothermap)
            cout << X.first << " " << X.second << " ";
        cout << endl;

    }   
    //.key_comp()用于返回一个用于比较键值的对象的副本;
    //.key_compare key_comp()const;
    {
        map<char,int> mymap;
        map<char,int>::key_compare mycomp = mymap.key_comp();

        mymap['a']=100;
        mymap['b']=101;
        mymap['c']=102;
        cout << "mymap contains: ";
        char highest =mymap.rbegin()->first;

        map<char,int>::iterator it = mymap.begin();
        do{
            cout << it->first << "=>" << it->second << " ";
        }while(mycomp((*it++).first,highest));
        cout <<'\n';

    }

    //.lower_bound()包含有两种形式:iterator lower_bound(const key_type& k);
    //const_iterator lower_bound(const key_type& k) const;

    {
        map<char,int> mymap;
        map<char,int>::iterator itlow,itup;
        mymap['1']=16;
        mymap['2']=19;
        mymap['3']=20;
        mymap['4']=19;
        mymap['5']=17;

        itlow = mymap.lower_bound('3');
        itup = mymap.upper_bound('3');
        mymap.erase(itlow,itup);

        for(auto X:mymap){
            cout << X.first << "=>" << X.second << " ";
        }
        cout << endl;

    }

    //.max_size()用于返回当前容器可以存放的最大值;
    //size_type max_size() const noexcept;
    //


    //.operator=:用于实现两个不同的map对象进行赋值;
    //map& operator=(const map& x);拷贝
    //map& operator=(const map&& x);移动
    //map& operator=(initializer_list<value_type> il);初始化列表

    {
        map<char,int> first;
        map<char,int> second;
        first['x']=1;
        first['y']=2;
        first['z']=3;
        first['a']=4;
        first['b']=5;

        cout << "first contains: ";

        for(auto X:first)
            cout << "[" << X.first << ":" << X.second << " ";
        cout << endl;
        second = first;

        for(auto X:second)
            cout << "[" << X.first << ":" << X.second << " ";
        cout << endl;

        first = map<char,int>();
        cout << first.size() << endl;
    }

    //.mapped_type& operator[](const key_type& k);
    //mapped_type& operator[](const key_type&& k);


    //.swap()用于快速的交换两个map结构

    mymap1.swap(mymap1);

    cout << "mymap1 contains: ";
    for(auto X:mymap1)
        cout << "[" << X.first << ":" << X.second << "] ";
    cout << endl;
    cout << "mymap2 contains: ";
    for(auto X:mymap2)
        cout << "[" << X.first << ":" << X.second << "] ";
    cout << endl;


    //.upper_bound() 
    //iterator upper_bound(const key_type& k);
    //const_iterator upper_bound(const key_type& k)const;


    //.value_comp():value_compare value_comp() const;

    {
        map<char,int> mymap;
        mymap['a'] = 1001;
        mymap['b'] = 1002;
        mymap['c'] = 1003;
        auto highest = *mymap.rbegin();
        auto it = mymap.begin();
        cout << "mymap value contains: ";
        do{
            cout << it->first << "=>" << it->second << " ";
        }while(mymap.value_comp()(*it++,highest));

    }
    return 0;

}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值