STL中map用法以及示例

STL中map用法以及示例

map简介

 map是一类关联式容器。它的特点是增加和删除节点对迭代器的影响很小,除了那个操作节点,对其他的节点都没有什么影响;
对数据自动排序的功能;
可以修改实值,而不能修改key。

使用map

使用map得包含map类所在的头文件

#include <map>  //注意,STL头文件没有扩展名.h

 map对象是模板类,需要关键字和存储对象两个模板参数:

std:map<int,string> mapTest;

这样就定义了一个用int作为索引,并拥有相关联的指向string的指针.

为了使用方便,可以对模板类进行一下类型定义。

typedef map<int,CString> UDT_MAP_INT_CSTRING;
UDT_MAP_INT_CSTRING enumMap;

数据的插入

数据的插入有三种方式(示例见最后)

第一种:用insert函数插入pair数据

第二种:用insert函数插入value_type数据

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

以上三种用法,虽然都可以实现数据的插入,但是它们是有区别的,当然了第一种和第二种在效果上是完成一样的,用insert函数插入数据,在数据的 插入上涉及到集合的唯一性这个概念,即当map中有这个关键字时,insert操作是插入数据不了的,但是用数组方式就不同了,它可以覆盖以前该关键字对应的值

map的大小

在往map里面插入了数据,我们怎么知道当前已经插入了多少数据呢,可以用size函数,用法如下:

Int nSize = mapStudent.size();

数据的遍历

这里也提供三种方法,对map进行遍历

第一种:应用前向迭代器

第二种:应用反相迭代器

第三种,用数组的形式

查找并获取map中的元素

find函数

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

 查找map中是否包含某个关键字条目用find()方法,传入的参数是要查找的key,在这里需要提到的是begin()和end()两个成员,
分别代表map对象中第一个条目和最后一个条目,这两个数据的类型是iterator。

lower_bound函数 和 upper_bound函数

lower_bound函数用法,这个函数用来返回要查找关键字的下界(是一个迭代器)

upper_bound函数用法,这个函数用来返回要查找关键字的上界(是一个迭代器)
如果上界和下界相等则说明map中没有这个关键字

从map中删除元素

移除某个map中某个条目用erase()

该成员方法的定义如下:

iterator erase(iterator it);//通过一个条目对象删除

iterator erase(iterator first,iteratorlast)//删除一个范围

size_type erase(const Key&key);//通过关键字删除

clear()//就相当于enumMap.erase(enumMap.begin(),enumMap.end());

慎用erase的返回值

**STL容器分Sequence Container(vector,deque,list)和Associative
Container(set,multiset,map,multimap)。
C++标准中,Sequence Container的erase函数会返回iterator,但Associative
Container不返回iterator。**

示例

#include <map>  
#include <string>  
#include <iostream>  

using namespace std;  

int main()  
{  
    map<int, string> mapTest;
    pair<map<int,string>::iterator,bool> Insert_pair;

    mapTest.insert(pair<int, string>(1, "string_one"));
    mapTest.insert(pair<int, string>(2, "string_two"));  
    mapTest.insert(pair<int, string>(3, "string_three"));  
    mapTest.insert(make_pair(4,"string_four"));
    mapTest.insert(make_pair(5,"string_five"));
    /*当map中有这个关键字时,insert操作是插入不了数据的*/
    Insert_pair = mapTest.insert(make_pair(5,"string_five_new"));
    if(Insert_pair.second == false)
    {
        cout << "mapTest[5] insert failed !\n"<< endl;
    }

    mapTest.insert(map<int, string>::value_type (6, "string_six"));
    /*当map中有这个关键字时,insert操作是插入不了数据的*/
    Insert_pair = mapTest.insert(map<int, string>::value_type (6, "string_six_new"));
    if(Insert_pair.second == false)
    {
        cout << "mapTest[6] insert failed !\n"<< endl;
    }

    mapTest[7] = "string_seven";
    /*数组可以直接覆盖该关键字对应的值*/
    mapTest[7] = "string_seven_new";

    mapTest[8] = "string_eight";
    mapTest[9] = "string_nine";        
    mapTest[10] = "string_ten";

/*****************************************************************************
****************************  遍历数据      **************************************
*****************************************************************************/
    map<int, string>::iterator iter;  
    for(iter = mapTest.begin(); iter != mapTest.end(); iter++)  
       cout << iter->first << " " << iter->second << endl;  
    cout << "------------------------------------------------\n"<< endl;

    map<int,string>::reverse_iterator iter1;
    for(iter1 = mapTest.rbegin(); iter1 != mapTest.rend(); iter1++)
        cout << iter1->first << " " << iter1->second << endl;

    cout << "------------------------------------------------\n"<< endl;
    int len = mapTest.size();
    cout << "len = " << len << endl;
    for(int i = 1;i <= len; i++)
    {
        cout << i << "," << mapTest[i] << endl;
    }
    cout << "------------------------------------------------\n"<< endl;
/*****************************************************************************
****************************  查找数据      **************************************
*****************************************************************************/
    iter = mapTest.find(7);
    if(iter != mapTest.end())
        cout << "find:" << iter->first << " " << iter->second << endl;
    else
        cout << "not find \n" << endl;

    /*返回下界迭代器*/
    iter = mapTest.lower_bound(1);
    if(iter == mapTest.begin())
        cout << "can not find lower_bound!\n" << endl;
    else
        cout << "find:" << iter->first << " " << iter->second << endl;
    /*返回上界迭代器*/
    iter = mapTest.upper_bound(7);
    if(iter == mapTest.end())
        cout << "can not find upper_bound!\n" << endl;
    else
        cout << "find:" << iter->first << " " << iter->second << endl;
    cout << "------------------------------------------------\n"<< endl;
    /*****************************************************************************
    ****************************  删除数据      **************************************
    *****************************************************************************/

    /*STL容器分Sequence Container(vector,deque,list)和Associative 
    Container(set,multiset,map,multimap)。C++标准中,Sequence 
    Container的erase函数会返回iterator,但Associative 
    Container不返回iterator。*/

    map<int,string>::iterator iter2;

    iter = mapTest.find(3);
    mapTest.erase(iter);//慎用返回值
    int num = mapTest.erase(4);
    cout << "erase:" << num << endl;


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


    iter = mapTest.find(2);
    iter2 = mapTest.find(8);
    mapTest.erase(iter,iter2);//慎用返回值


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

    cout << "------------------------------------------------\n"<< endl;
    return 0;
}  
  • 7
    点赞
  • 21
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值