sstack.cpp

来自「Thinking in C++ 2nd edition source code 」· C++ 代码 · 共 60 行

CPP
60
字号
//: C08:SStack.cpp
// From Thinking in C++, 2nd Edition
// at http://www.BruceEckel.com
// (c) Bruce Eckel 1999
// Copyright notice in Copyright.txt
// enum inside classes
#include <cstring>
#include <iostream>
using namespace std;

class StringStack {
  enum { size = 100 };
  const char* Stack[size];
  int index;
public:
   StringStack();
   void push(const char* s);
   const char* pop();
};

StringStack::StringStack() : index(0) {
  memset(Stack, 0, size * sizeof(char*));
}

void StringStack::push(const char* s) {
  if(index < size)
    Stack[index++] = s;
}

const char* StringStack::pop() {
  if(index > 0) {
    const char* rv = Stack[--index];
    Stack[index] = 0;
    return rv;
  }
}

const char* iceCream[] = {
  "pralines & cream",
  "fudge ripple",
  "jamocha almond fudge",
  "wild mountain blackberry",
  "raspberry sorbet",
  "lemon swirl",
  "rocky road",
  "deep chocolate fudge"
};

const int ICsz = 
  sizeof iceCream / sizeof *iceCream;

int main() {
  StringStack SS;
  for(int i = 0; i < ICsz; i++)
    SS.push(iceCream[i]);
  const char* cp;
  while((cp = SS.pop()) != 0)
    cout << cp << endl;
} ///:~

⌨️ 快捷键说明

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