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。

原文地址:https://www.cnblogs.com/tupx/p/3434160.html