zoukankan      html  css  js  c++  java
  • map.insert(value_type)为什么后面插进的value会覆盖前面的value

    看这段代码:

    ...
            map<char, wchar_t*>   mapAllVolumes;
        vector<wchar_t*>      vecCheckedVolumes;
        CString               cstrVolDesc;
        

        for(int i = 'c'; i < 'z'+1; i++)
        {
            cstrVolDesc.Format(L"%c:Volume %c", i, i);
            wchar_t *label = cstrVolDesc.GetBuffer();
            

             mapAllVolumes.insert(std::pair<char,wchar_t*>(toupper(i), label));  //key using UpperCase

        }
    ...
    结果:map里面所有key对应的都是最后一个插入的value


    再看下一段代码:
        CString str1;
        str1 = "C::::::::::";
        mapAllVolumes.insert(map<char,wchar_t*>::value_type('C', str1.GetBuffer()));
        str1= "D:::::::::";
        mapAllVolumes.insert(map<char,wchar_t*>::value_type('D', str2.GetBuffer()));
        mapAllVolumes.insert(map<char,wchar_t*>::value_type('E', L"E:::"));

    结果:前两个key对应的value相同。
    由此可以猜想:map是引用了穿进去的参数地址。

    稍微修改下就可证明:
        CString str1;
        str1 = "C::::::::::";
        mapAllVolumes.insert(map<char,wchar_t*>::value_type('C', str1.GetBuffer()));
        CString str2 = "D:::::::::";
        mapAllVolumes.insert(map<char,wchar_t*>::value_type('D', str2.GetBuffer()));
        mapAllVolumes.insert(map<char,wchar_t*>::value_type('E', L"E:::"));
    这样就OK.

    第一段代码的原因是CString.GetBuffer() 直接返回了m_pszData,而map会引用传进去的地址,所以会导致所有的key对应相同的value


    解决方法:
    每次给要传进去的value申请新空间,并把value拷贝进去。
        for(int i = 'c'; i < 'z'+1; i++)
        {
            cstrVolDesc.Format(L"%c:Volume %c", i, i);
            wchar_t *label = new wchar_t[100];
        
            int nLen = cstrVolDesc.GetLength();
            int len = wcslen(label);
            
            wcscpy_s(label, cstrVolDesc.GetLength()+1, cstrVolDesc.GetString());

             mapAllVolumes.insert(std::pair<char,wchar_t*>(toupper(i), label));  //key using UpperCase

        }

    总结:map的insert方法每次传进去的value必须保证是不同的内存地址。否则就会覆盖前面使用相同地址value的key。

  • 相关阅读:
    c++ readIntger writeIntger
    c++ string split function
    uniapp 创建简单的tabs
    c++ 获取和设置 窗口标题
    两种js数组去重的方法
    将jquery和公共样式缓存到localStorage,可以减少Http请求,从而优化页面加载时间
    H5 多个视频 循环播放效果
    使用iScroll时,input等不能输入内容的解决方法
    修改EsayUi 中 tree 的原有样式,变为according 之类的样式 ,且子菜单显示在右侧
    跨浏览器的事件对象-------EventUtil 中的方法及用法
  • 原文地址:https://www.cnblogs.com/tupx/p/3434160.html
Copyright © 2011-2022 走看看