C++ examples for STL:vector
Storing Pointers in a vector
#include <iostream> #include <vector> using namespace std; static const int NUM_OBJECTS = 10; class MyClass { /*...*/ }; int main() {/* ww w .ja v a 2 s . c o m*/ vector<MyClass*> vec; MyClass* p = NULL; // Load up the vector with MyClass objects for (int i = 0; i < NUM_OBJECTS; i++) { p = new MyClass(); vec.push_back(p); } for (vector<MyClass*>::iterator pObj = vec.begin(); pObj != vec.end(); ++pObj) { delete *pObj; // deleting what pObj points to, which is a pointer } vec.clear(); }