int main() { PStash intStash; // 'new' works with built-in types,too for(int i = 0; i < 25; i++) intStash.add(new int(i)); // store the int object in heap and initialize it with i for(int j = 0; j< intStash.count(); j++) cout << "intStash[" << j << "] = " << *(int*)intStash[j] <<endl; //clean up for(int k = 0; k < intStash.count(); k++) delete (int*)intStash.remove(k); ifstream in ("PStashTest.cpp"); PStash stringStash; string line; while(getline(in, line)) stringStash.add(new string(line)); //print out for(int u = 0; stringStash[u]; u++) cout << "stringStash[" << u << "] = " << *(string*)stringStash[u] << endl; //clean up for(int v = 0; v < stringStash.count(); v++) delete (string*)stringStash.remove(v); } ///:~
int main() { PStash<AutoCounter> acStash; for(int i = 0; i < 10; i++) acStash.add(AutoCounter::create()); cout << "Removing 5 manually:" << endl; for(int j = 0; j < 5; j++) delete acStash.remove(j); cout << "Remove two without deleting them:" << endl; // ... to generate the cleanup error message. cout << acStash.remove(5) << endl; cout << acStash.remove(6) << endl; cout << "The destructor cleans up the rest:" << endl; // Repeat the test from earlier chapters: ifstream in("TPStashTest.cpp"); assure(in, "TPStashTest.cpp"); PStash<string> stringStash; string line; while(getline(in, line)) stringStash.add(new string(line)); // Print out the strings: for(int u = 0; stringStash[u]; u++) cout << "stringStash[" << u << "] = " << *stringStash[u] << endl; } ///:~
int main() { ifstream in("stash_test.cpp"); PStash<string, 10> ps; string line; while(getline(in, line)) { ps.add(new string(line)); } int num = ps.count(); for(int j = 0; j < num; j++) { cout << *ps[j] << endl; delete ps.remove(j); } cout << "done" << endl; }