c++ map的使用方法

 

/* ***********************************************************************
*
*  Map的特点:  1、存储Key-value对
*              2、支持快速查找,查找的复杂度基本是Log(N)
*              3、快速插入,快速删除,快速修改记
*
/***********************************************************************
*/
#include 
< stdio.h >
#pragma  warning(disable:4786)
#include 
< string >
#include 
< map >     // 包含头文件
using   namespace  std;

// 输出map中的记录
#define  PRINTMAP(iterSuffix, mapName)\
{\
    PRJ_MAP_STRING2INT_WORDREC::iterator iter##iterSuffix 
=  mapName##.begin();\
    
for  (; iter##iterSuffix  !=  mapName##.end();  ++ iter##iterSuffix)\
    {\
        printf(
" \"%s\" -> total: %d\n " , iter##iterSuffix -> first.c_str(), iter##iterSuffix -> second);\
    }\
    printf(
" \n " );\
}
/* ***********************************************************************
* 数据类型定义
/***********************************************************************
*/
typedef map
< string int >  PRJ_MAP_STRING2INT_WORDREC; // 定义map类型的别名

/* ********************************************************************** */
/*  全局函数
/***********************************************************************
*/
void  main()
{
    map
< string int >  mapWordRecPrep;             /*  定义map类型的变量  */
    PRJ_MAP_STRING2INT_WORDREC mapWordRecVerb;  
/*  用别名定义map类型的变量  */

    
// 插入记录
    mapWordRecPrep[ " the " ]   =   100 /*  数组方式  */
    mapWordRecPrep[
" so " ]    =   50 ;

    mapWordRecVerb[
" find " =   1 ;
    mapWordRecVerb[
" seen " =   2 ;
    mapWordRecVerb[
" jump " =   3 ;
    mapWordRecVerb[
" swim " =   4 ;
    mapWordRecVerb.insert(map
< string int > ::value_type( " look " 5 ));  /*  value_type方式  */
    mapWordRecVerb.insert(pair
< string int > ( " walk " 6 ));  /*  pair方式  */

    
/*  value_type和pair方式不出现覆盖现象  */
    printf(
" Insert method: value_type\n " );
    pair
< map < string int > ::iterator,  bool >  inserted;
    inserted 
=  mapWordRecVerb.insert(map < string int > ::value_type( " walk " 7 ));
    printf(
" %s\n " true   ==  inserted.second  ?   " Insert success! "  :  " Insert failed! " );
    PRINTMAP(Ver, mapWordRecVerb);

    
/*  数组方式出现覆盖现象 */
    printf(
" Insert method: array\n " );
    mapWordRecVerb[
" walk " =   7 ;
    PRINTMAP(Ver, mapWordRecVerb);

    
// 查找记录
    map < string int > ::iterator iter  =  mapWordRecPrep.find( " so " );
    printf(
" %s\n " , iter  ==  mapWordRecPrep.end()  ?   " Not find! "  :  " Find! " );

    
// 删除记录
    map < string int > ::iterator iterV  =  mapWordRecVerb.find( " seen " );
    
if  (iterV  !=  mapWordRecVerb.end())
    {
        mapWordRecVerb.erase(iterV);              
/*  迭代器方式删除  */
        printf(
" \nDelete word \"seen\" done!\n " );
        PRINTMAP(Verb, mapWordRecVerb);
    }

    
int  n  =  mapWordRecVerb.erase( " swim " );          /*   关键字方式删除  */
    printf(
" \nDelete word \"swim\" done!\n " );
    PRINTMAP(Verb, mapWordRecVerb);

    mapWordRecVerb.erase(mapWordRecVerb.begin(), mapWordRecVerb.end()); 
/*  成片删除  */
    printf(
" \nDelete all word done!\n " );
    PRINTMAP(Verb, mapWordRecVerb);

    
// map的其它函数
     if  (mapWordRecVerb.empty()  &&   0   ==  mapWordRecVerb.size())    /*  判断map是否为空  */
    {
        printf(
" mapWordRecVerb is empty!\n\n " );
    }

    PRINTMAP(Pre, mapWordRecPrep)
    mapWordRecPrep.clear();                                     
/*  清空map  */
    PRINTMAP(Pre, mapWordRecPrep)
    
if  (mapWordRecPrep.empty()  &&   0   ==  mapWordRecPrep.size())
    {
        printf(
" mapWordRecPrep is empty!\n\n " );
    }
}

 

输出结果:

Insert method: value_type
Insert failed!
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"seen" -> total: 2
"swim" -> total: 4
"walk" -> total: 6

Insert method: array
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"seen" -> total: 2
"swim" -> total: 4
"walk" -> total: 7

Find!

Delete word "seen" done!
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"swim" -> total: 4
"walk" -> total: 7


Delete word "swim" done!
"find" -> total: 1
"jump" -> total: 3
"look" -> total: 5
"walk" -> total: 7


Delete all word done!

mapWordRecVerb is empty!

"so" -> total: 50
"the" -> total: 100


mapWordRecPrep is empty!

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值