【发布时间】:2016-03-18 10:42:31
【问题描述】:
这是我程序中的代码
#include <iostream>
#include <string>
#include <list>
int main()
{
std::list<int *> list;
//populating the list with some pointers
for(int i =0;i<5;i++)
{
int *p = new int(i);
list.push_back(p);
}
//want to delete and erase the contents of the above list
for(std::list<int *>::iterator iter = list.begin(); iter != list.end(); iter++)
{
delete(*iter);
list.erase(--iter);
}
// trying to print the list contents
for(std::list<int *>::iterator iter = list.begin(); iter != list.end(); iter++)
{
std::cout<<*iter<<std::endl;
}
}
这是一个正确的方法吗?
【问题讨论】:
标签: c++ list pointers iterator