C++ 遍历清空指针元素的代码

template<typename T>
	static void clear_list(list<T*> & itemList) {
		cout<<"clear list start" <<endl;
		typename list<T*>::iterator it;
		if (itemList.size() == 0) {
			cout<<"clear list end" <<endl;
			return;
		}
		for (it = itemList.begin(); it != itemList.end();) {
			T* p = *it;
			it = itemList.erase(it);
			cout<<"delete "<< p->id <<endl;
			delete p;
		}
		cout<<"clear list end" <<endl;
	}


网上看到的erase后对it++的操作的示例代码有问题,不知道是不是mingw的实现库跟stl有差别。
条件清空
template<typename T>
	static void clear_list(list<T*> & itemList) {
		cout<<"clear list start" <<endl;
		typename list<T*>::iterator it;
		if (itemList.size() == 0) {
			cout<<"clear list end" <<endl;
			return;
		}
		for (it = itemList.begin(); it != itemList.end();) {
                           
			T* p = *it;
                           if (p->state=="offline") {
			it = itemList.erase(it);
			cout<<"delete "<< p->id <<endl;
			delete p;
                           } else {
                              ++it;
                           }
		}
		cout<<"clear list end" <<endl;
	}

你可能感兴趣的:(C++ 遍历清空指针元素的代码)