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。

  • 相关阅读:
    u-boot mkconfig文件分析
    uboot的lds文件分析
    gitlab webhook jenkins 403问题解决方案
    【python】将json串写入文件,并以json格式读取出来
    sqlalchemy 中 desc 的使用
    【mysql】如何通过navicat配置表与表的多对一关系,一对一关系?设计外键的效果
    【mysql】一对一关系的理解,以及Navicat Premium怎么设置字段的唯一性(UNIQUE)?
    【mysql】时间类型-如何根据不同的应用场景,选择合适的时间类型?
    Navicat Premium Mac 12 破解方法-亲测成功
    【linux】cp 批量复制文件
  • 原文地址:https://www.cnblogs.com/tupx/p/3434160.html
Copyright © 2011-2022 走看看