safearraytester.cpp

来自「为SSD5课程《数据结构与算法》中的练习」· C++ 代码 · 共 41 行

CPP
41
字号
#include <stdexcept>
#include <iostream>
#include <cstdlib>

using namespace std;

#include "safearray.h"

int main (int argc, char *argv[]) {

  safearray<int> A(5);

  int index = 0;

  for (index=0; index < 5; index++) {
    A[index] = index;
  }

  for (index=0; index < 5; index++) {
    cout << A[index] << endl;
  }

  /*!Begin Snippet*/
  try {
    cout << A[-1] << endl; // attempt to read
  }
  catch (out_of_range& e) {
    cerr << e.what() << endl; // "Index is below 0"
  }

  try {
    A[100] = 1;  // attempt to write
  }
  catch (out_of_range& e) {
    cerr << e.what() << endl; // "Index is too high"
  }
  /*!End Snippet*/
  
  return EXIT_SUCCESS;
}

⌨️ 快捷键说明

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