map练习

#include<cstdio>
#include<iostream>
#include<map>
#include<cstring>
using namespace std;

int main()
{
    map<int,string>mp1;
    mp1.insert(pair<int,string>(1,"qaq"));
    mp1.insert(pair<int,string>(2,"qwq"));
    mp1.insert(pair<int,string>(3,"qeq"));
    cout<<mp1.size()<<endl;

    map<int,string>::iterator d;
    d=mp1.find(3);
    if(d!=mp1.end())
    {
        cout<<d->second<<endl;
    }
    cout<<endl;
    map<int,string>::iterator a;
    for(a=mp1.begin();a!=mp1.end();a++)
    {
        cout<<a->first<<" ";
        cout<<a->second<<" ";
        cout<<endl;
    }

    map<int,string>::reverse_iterator b;
    for(b=mp1.rbegin();b!=mp1.rend();b++)
    {
        cout<<(*b).first<<" ";
        cout<<(*b).second<<" ";
        cout<<endl;
    }
    cout<<endl;
    map<int,char>mp2;
    mp2[3]=('a');
    mp2[1]=('b');
    mp2[2]=('c');
    for(map<int,char>::iterator c=mp2.begin();c!=mp2.end();c++)
    {
        cout<<(*c).first<<" ";
        cout<<(*c).second<<" ";
        cout<<endl;
    }


    for(int i=1;i<=mp2.size();i++)
    {
        cout<<mp2[i]<<endl;
    }
    return 0;
}

//  map的基本操作函数:
//
//     C++ maps是一种关联式容器,包含“关键字/值”对
//
//     begin()         返回指向map头部的迭代器
//
//     clear()        删除所有元素
//
//     count()         返回指定元素出现的次数
//
//     empty()         如果map为空则返回true
//
//     end()           返回指向map末尾的迭代器
//
//     equal_range()   返回特殊条目的迭代器对
//
//     erase()         删除一个元素
//
//     find()          查找一个元素
//
//     get_allocator() 返回map的配置器
//
//     insert()        插入元素
//
//     key_comp()      返回比较元素key的函数
//
//     lower_bound()   返回键值>=给定元素的第一个位置
//
//     max_size()      返回可以容纳的最大元素个数
//
//     rbegin()        返回一个指向map尾部的逆向迭代器
//
//     rend()          返回一个指向map头部的逆向迭代器
//
//     size()          返回map中元素的个数
//
//     swap()           交换两个map
//
//     upper_bound()    返回键值>给定元素的第一个位置
//
//     value_comp()     返回比较元素value的函数



猜你喜欢

转载自blog.csdn.net/zhaobaole2018/article/details/84343747