【发布时间】:2013-10-02 02:37:07
【问题描述】:
关于 http://www.cplusplus.com/reference/list/list/erase/ 的描述,我明白了
This effectively reduces the container size by the number of elements removed,
which are destroyed.
pop 方法的类似描述。
但在我对 VS2012 的测试中,删除的项目并没有被破坏。我对此感到困惑。
这是我的测试代码。我仍然可以从 std::list 擦除的内存中输出,我认为这是错误的。
#include <stdio.h>
#include <list>
using namespace std;
class test
{
public:
int a;
test() {a = 111;}
};
int main(void)
{
list<test*> l;
test* t = new test;
l.push_back(t);
l.erase(l.begin());
printf("%d\n", t->a);
return 0;
}
【问题讨论】:
-
也许您应该先学习基础知识(指针的含义等),然后再使用
std::list这样的“高级”功能? -
你期望你的错误代码会发生什么?如果您期待某种错误消息或崩溃,那么恐怕您会对 C++ 感到非常失望。大多数错误会导致 Undefined Behaviour,这意味着实际上任何事情都可能发生。