Exemple #1
0
int main() {
#if 1
  PStash<AutoCounter> acStash;

  for(int i = 0; i < 100; 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;
       */
#else
  // 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;
#endif
} ///:~
Exemple #2
0
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* ps = new PStash();
  int loop = 20;
  int i = 0;
  int* ip = NULL;
  for(; i < loop; ps->add(new int(i++)));
  cout << "[ ";
  for(i = 0; i < ps->count(); i++) {
    ip = (int*)((*ps)[i]);
    cout << ip << "=" << *ip;
    if(i < loop - 1) cout << ", ";
  }
  cout << " ]" << endl;
  delete ps;
}
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;
}