zoukankan      html  css  js  c++  java
  • c++ map的使用方法(转自http://www.cnblogs.com/JCSU/articles/1996876.html)

    /************************************************************************
    *
    *  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
    <stringint> PRJ_MAP_STRING2INT_WORDREC;//定义map类型的别名

    /************************************************************************/
    /* 全局函数
    /***********************************************************************
    */
    void main()
    {
        map
    <stringint> 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
    <stringint>::value_type("look"5)); /* value_type方式 */
        mapWordRecVerb.insert(pair
    <stringint>("walk"6)); /* pair方式 */

        
    /* value_type和pair方式不出现覆盖现象 */
        printf(
    "Insert method: value_type\n");
        pair
    <map<stringint>::iterator, bool> inserted;
        inserted 
    = mapWordRecVerb.insert(map<stringint>::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<stringint>::iterator iter = mapWordRecPrep.find("so");
        printf(
    "%s\n", iter == mapWordRecPrep.end() ? "Not find!" : "Find!");

        
    //删除记录
        map<stringint>::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!

  • 相关阅读:
    HTML表单
    CSS等高布局的6种方式
    HTML用户反馈表单
    HTML美化修饰<A>
    sql查询语句 --------一个表中的字段对应另外一个表中的两个字段的查询语句
    jq 表格添加删除行
    js 静止f1到f12 和屏蔽鼠标右键
    手机自适应页面的meta标签
    tp3.2 的验证码的使用
    php多线程抓取网页
  • 原文地址:https://www.cnblogs.com/cumtb3S/p/2011014.html
Copyright © 2011-2022 走看看