stl map key 为指针时delete指针的问题

bingcaihuang bingcaihuang 墨衍会员 · AI 创作全网分发 墨衍智能分发 通过墨衍一键同步至各平台 可同步平台 CSDN微信公众号微博知乎掘金百家号博客园抖音小红书 你的文章也可以这样分发 墨衍会员 ¥399起/年,写一次发全网 我也要 + 加入社区
代码如下:

struct cmp_str
{
bool operator()(const TCHAR *a, const TCHAR *b) const
{
return _tcscmp(a, b) < 0;
}
};

typedef std::map<const TCHAR*, int, cmp_str> TESTMAP

const TCHAR* ch1 = new TCHAR[_tcslen(_T("one")) + 1];
const TCHAR* ch2 = new TCHAR[_tcslen(_T("two")) + 1];
const TCHAR* ch3 = new TCHAR[_tcslen(_T("three")) + 1];
const TCHAR* ch4 = new TCHAR[_tcslen(_T("four")) + 1];
const TCHAR* ch5 = new TCHAR[_tcslen(_T("five")) + 1];

_tcsncpy(ch1, _T("one"), (_tcslen(_T("one")) + 1) * sizeof(TCHAR));
_tcsncpy(ch1, _T("two"), (_tcslen(_T("two")) + 1) * sizeof(TCHAR));
_tcsncpy(ch1, _T("three"), (_tcslen(_T("three")) + 1) * sizeof(TCHAR));
_tcsncpy(ch1, _T("four"), (_tcslen(_T("four")) + 1) * sizeof(TCHAR));
_tcsncpy(ch1, _T("five"), (_tcslen(_T("five")) + 1) * sizeof(TCHAR));

TESTMAP mapper;

mapper[ch1] = 1;
mapper[ch2] = 2;
mapper[ch3] = 3;
mapper[ch4] = 4;
mapper[ch5] = 5;

/* do what you want */

delete []ch1;
delete []ch2;
delete []ch3;
delete []ch4;
delete []ch5;



do what you want 以后想delete申请的内存,此时报错_CrtIsValidHeapPointer,难道key只能用string?不能用pointer?如果要释放这些内存该怎么处理啊?坐等高手指教
...全文

相关推荐

评论 17
收藏 已收藏
分享
返回顶部
bingcaihuang
C++ 语言 65,214 成员 250,514 内容