⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 tstashtest.cpp

📁 Think in C++ 第二版源码
💻 CPP
字号:
//: C16:TStashTest.cpp

// From Thinking in C++, 2nd Edition

// Available at http://www.BruceEckel.com

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Test TStash

#include "TStash.h"

#include "../require.h"

#include <fstream>

#include <vector>

#include <string>

using namespace std;

ofstream out("tstest.out");



class Int {

  int i;

public:

  Int(int ii = 0) : i(ii) {

    out << ">" << i << endl;

  }

  ~Int() { out << "~" << i << endl; }

  operator int() const { return i; }

  friend ostream&

    operator<<(ostream& os, const Int& x) {

      return os << x.i;

  }

};



int main() {

  TStash<Int> intStash; // Instantiate for Int

  for(int i = 0; i < 30; i++)

    intStash.add(new Int(i));

  TStashIter<Int> intIter(intStash);

  intIter.forward(5);

  for(int j = 0; j < 20; j++, intIter++)

    intIter.remove(); // Default removal

  for(int k = 0; k < intStash.count(); k++)

    if(intStash[k]) // Remove() causes "holes"

      out << *intStash[k] << endl;



  ifstream file("TStashTest.cpp");

  assure(file, "TStashTest.cpp");

  // Instantiate for String:

  TStash<string> stringStash;

  string line;

  while(getline(file, line))

    stringStash.add(new string(line));

  for(int u = 0; u < stringStash.count(); u++)

    if(stringStash[u])

      out << *stringStash[u] << endl;

  TStashIter<string> it(stringStash);

  int n = 25;

  it.forward(n);

  while(it) {

    out << n++ << ": " << it->c_str() << endl;

    it++;

  }

} ///:~

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -