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

📄 dynatrash.cpp

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

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

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

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

//{L} TrashPrototypeInit

//{L} fillBin Trash TrashStatics

// Using a map of vectors and RTTI

// to automatically sort Trash into

// vectors. This solution, despite the

// use of RTTI, is extensible.

#include "Trash.h"

#include "fillBin.h"

#include "sumValue.h"

#include "../purge.h"

#include <iostream>

#include <fstream>

#include <vector>

#include <map>

#include <typeinfo>

using namespace std;

ofstream out("DynaTrash.out");



// Must adapt from type_info in Standard C++,

// since type_info is too restrictive:

template<class T> // T should be a base class

class TypeInfo {

  string id;

public:

  TypeInfo(T* t) : id(typeid(*t).name()) {}

  const string& name() { return id; }

  friend bool operator<(const TypeInfo& lv,

    const TypeInfo& rv){

    return lv.id < rv.id;

  }

};



class TypeMap : 

  public map<TypeInfo<Trash>, vector<Trash*> >,

  public Fillable {

public:

  // Satisfies the Fillable interface:

  void addTrash(Trash* t) {

    (*this)[TypeInfo<Trash>(t)].push_back(t);

  }

  ~TypeMap() {

    for(iterator it = begin(); it != end(); it++)

      purge((*it).second);

  }

};



int main() {

  TypeMap bin;

  fillBin("Trash.dat", bin); // Sorting happens

  TypeMap::iterator it;

  for(it = bin.begin(); it != bin.end(); it++)

    sumValue((*it).second);

} ///:~

⌨️ 快捷键说明

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