#include "stdafx.h"
#include <list>
#include <iostream>
using namespace std;
list<int*> testList;
void delNode(int * node)
{
list<int*>::iterator it = testList.begin();
list<int*>::iterator itEnd = testList.end();
for (; it != itEnd; )
{
if (*it == node)
{
list<int *>::iterator tmpIT = it++; //erase可能会导致迭代器失效,故做此处理。
testList.erase(tmpIT);
}else {
it++;
}
}
}
void delNodeFunc2(int * node)
{
list<int * > ::iterator findNode = find(testList.begin(), testList.end(), node);
if (findNode != testList.end()) {
testList.erase(findNode);
}
}
void printList()
{
list<int *>::iterator it = testList.begin();
for (; it!=testList.end(); it++)
{
cout << *(*it) << endl;
}
}
int main()
{
int a = 100;
int b = 200;
int c = 300;
testList.push_back(&a);
testList.push_back(&b);
testList.push_back(&c);
cout << "before testList.size = " << testList.size() << endl;
printList();
delNode(&b);
delNodeFunc2(&a);
cout <<"after testList.size = " <<testList.size() << endl;
printList();
return 0;
}