• 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。

  • 相关阅读:
    触屏版轻量级分页插件jqPagination分享
    30行js让你的rem弹性布局适配所有分辨率(含竖屏适配)
    js适配根字体大小
    返回顶部
    44 | 套路篇:网络性能优化的几个思路(下)
    43 | 套路篇:网络性能优化的几个思路(上)
    42 | 案例篇:如何优化 NAT 性能?(下)
    41 | 案例篇:如何优化 NAT 性能?(上)
    40 | 案例篇:网络请求延迟变大了,我该怎么办?
    python排列组合之itertools模块
  • 原文地址:https://www.cnblogs.com/tupx/p/3434160.html
Copyright © 2020-2023  润新知