zoukankan      html  css  js  c++  java
  • stl std::map容器排序及使用注意事项 .

    01.#include "stdafx.h"   
    02.#include <map>   
    03.#include <iostream>   
    04.  
    05.int _tmain(int argc, _TCHAR* argv[])  
    06.{  
    07.    /** 
    08.     * map中的每个元素都是一个pair类型 
    09.     * 对于插入其中的元素都会默认按键值升序排列好 
    10.     */  
    11.      
    12.    std::map<int, int> m;  
    13.    m.insert(std::pair<int, int>(1, 20));  
    14.    m.insert(std::pair<int, int>(3, 90));  
    15.    m.insert(std::pair<int, int>(2, 60));  
    16.    m.insert(std::pair<int, int>(6, 40));  
    17.    m.insert(std::pair<int, int>(4, 50));  
    18.    m.insert(std::pair<int, int>(5, 10));  
    19.  
    20.    std::cout << "自动排序:" << std::endl;  
    21.    for(std::map<int, int>::iterator it=m.begin(); it!=m.end(); it++)  
    22.    {  
    23.        std::cout << it->first << "	" << it->second << std::endl;  
    24.    }  
    25.  
    26.  
    27.    std::cout << "自定义排序:" << std::endl;  
    28.      
    29.    struct s  
    30.    {     
    31.        int i;  
    32.        int j;  
    33.  
    34.        s(int a, int b):i(a), j(b)  
    35.        {  
    36.          
    37.        }  
    38.    };  
    39.  
    40.    //这个地方无论是结构体还是类都可以,   
    41.    //名字任意,只要与map中保持一致即可,   
    42.    //其中的函数签名必须是如下形式   
    43.    struct sortS  
    44.    {  
    45.        bool operator()(const s* ps1, const s* ps2)  
    46.        {  
    47.            return ps1->j >= ps2->j;  
    48.        }  
    49.      
    50.    };  
    51.  
    52.    std::map<s*, int, sortS> ms;  
    53.  
    54.    //之所以每个s前面加一个&符号是因为,map的第一个元素是一个s*类型   
    55.    ms.insert(std::make_pair(&s(3, 4), 2));  
    56.    ms.insert(std::make_pair(&s(5, 6), 1));  
    57.    ms.insert(std::make_pair(&s(7, 8), 4));  
    58.    ms.insert(std::make_pair(&s(9, 10), 3));  
    59.  
    60.    for(std::map<s*, int, sortS>::iterator it = ms.begin(); it!=ms.end(); ++it)  
    61.    {  
    62.        std::cout << it->first->i << "	" << it->first->j << "	" << it->second << std::endl;  
    63.    }  
    64.  
    65.  
    66.      
    67.    return 0;  
    68.}  

    需要注意的是:

    1.一旦一个键被插入map中,那么无论那个键被什么方式修改,它在map中的相对位置是不能改变的,如果修改了键值,map也会毫不知情,它对元素次序的假设将被推翻,查找合法元素的的操作会失败,iterator就不一定能按照键的次序来遍历map中的元素。

    2.在map中插入元素三种插入方式:

      (1) 、用insert方法插入pair对象:
       enumMap.insert(pair<int, Cstring>(1, “One”));
      (2)、用insert方法插入value_type对象:
       enumMap.insert(map<int, Cstring>::value_type (1, “One”));
      (3)、 用数组方式插入值:
       enumMap[1] = "One";
         enumMap[2] = "Two";
      第三种方式非常直观,但存在一个性能的问题。插入2时,先在enumMap中查找主键为2的项,没发现,然后将一个新的对象插入enumMap,键是2,值是一个空字符串,插入完成后,将字符串赋为"Two"; 该方法会将每个值都赋为缺省值,然后再赋为显示的值,如果元素是类对象,则开销比较大。用前两种方法可以避免开销

  • 相关阅读:
    C# 将数据导出到Excel汇总
    jquery 常用技巧
    JavaScript:世界上误解最深的语言
    对于jQuery中$.ajax方法的新认识
    JQuery上传插件Uploadify使用详解
    HTTP中Get与Post的区别
    Javascript中最常用的55个经典技巧
    C# params参数的应用
    10种JavaScript特效实例让你的网站更吸引人
    jQuery设计思想
  • 原文地址:https://www.cnblogs.com/yangxx-1990/p/5001399.html
Copyright © 2011-2022 走看看