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

📄 stash2.cpp

📁 Think in C++ 第二版源码
💻 CPP
字号:
//: C06:Stash2.cpp {O}

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

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

// (c) Bruce Eckel 1999

// Copyright notice in Copyright.txt

// Constructors & destructors

#include "Stash2.h"

#include <iostream>

#include <cassert>

using namespace std;

const int increment = 100;



Stash::Stash(int sz) {

  size = sz;

  quantity = 0;

  storage = 0;

  next = 0;

}



int Stash::add(void* element) {

  if(next >= quantity) // Enough space left?

    inflate(increment);

  // Copy element into storage,

  // starting at next empty space:

  int startBytes = next * size;

  unsigned char* e = (unsigned char*)element;

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

    storage[startBytes + i] = e[i];

  next++;

  return(next - 1); // Index number

}



void* Stash::fetch(int index) {

  assert(0 <= index && index < next);

  // Produce pointer to desired element:

  return &(storage[index * size]);

}



int Stash::count() {

  return next; // Number of elements in CStash

}



void Stash::inflate(int increase) {

  assert(increase > 0);

  int newQuantity = quantity + increase;

  int newBytes = newQuantity * size;

  int oldBytes = quantity * size;

  unsigned char* b = new unsigned char[newBytes];

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

    b[i] = storage[i]; // Copy old to new

  delete [](storage); // Old storage

  storage = b; // Point to new memory

  quantity = newQuantity;

}



Stash::~Stash() {

  if(storage != 0) {

   cout << "freeing storage" << endl;

   delete []storage;

  }

} ///:~

⌨️ 快捷键说明

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