code

#ifdef _DEBUG
#define DEBUG_CLIENTBLOCK   new( _CLIENT_BLOCK, __FILE__, __LINE__)
#else
#define DEBUG_CLIENTBLOCK
#endif
#define _CRTDBG_MAP_ALLOC
#include <crtdbg.h>
#ifdef _DEBUG
#define new DEBUG_CLIENTBLOCK
#endif

#include <iostream>
#include <map>
#include <string>

using namespace std;

class Example
{
public:
    Example();
    Example(int newIndex);
    virtual ~Example();
    int m_Index;
};

Example::Example()
{

}

Example::Example(int newIndex)
{
    m_Index = newIndex;
}

Example::~Example()
{
}



int main()
{
    std::map<std::string, Example*> MyTestMap;

    for (unsigned int i = 0; i < 10; ++i)
    {
        std::string tempStr = std::to_string(i);
        Example* pExample = new Example(i);
        MyTestMap[tempStr] = pExample;
    }

    std::map<std::string, Example*>::iterator iter = MyTestMap.begin();

    while (iter != MyTestMap.end())
    {
        delete iter->second;
        iter->second = NULL;
        iter = MyTestMap.erase(iter);
    }

    MyTestMap.clear();

    getchar();
    _CrtDumpMemoryLeaks();

    return 0;
}

如果您觉得对您有帮助,可以请站长喝一杯咖啡哦!

记得在赞赏备注里写上您的昵称

您可在本站资助名单中查看你的打赏记录哦!

支付宝扫一扫

微信扫一扫

金额随意,礼清义重

当前分类随机文章推荐

全站随机文章推荐

关于本站站长 StubbornHuang
C++ – std::map – 存储动态指针时正确释放内存-StubbornHuang Blog纵使晴明无雨色,入云深处亦沾衣。