prg5_3.cpp
来自「这是数据结构和算法的国外经典书籍.清华大学出版社出版的<数据结构C++语言」· C++ 代码 · 共 55 行
CPP
55 行
// File: prg5_3.cpp
// the program illustrates the use of exceptions with the miniVector
// class. during execution, the underflowError and indexRangeError
// exceptions each occur once
#include <iostream>
#include "d_vector.h"
using namespace std;
int main()
{
miniVector<int> v;
// try block attempts to erase frok empty vector; catch block
// catches underflowError exception from pop_back()
try
{
v.pop_back();
}
catch (const underflowError& e)
{
cout << e.what() << endl;
// store element in v[0]
v.push_back(99);
}
cout << "The size of v = " << v.size() << endl;
// try block enables index bound checking ; catch block catches
// indexRangeError exception from operator[]
try
{
cout << "v[0] = " << v[0] << endl;
cout << "v[1] = " << v[1] << endl;
}
catch (const indexRangeError& e)
{
cout << e.what() << endl;
}
return 0;
}
/*
Run:
miniVector pop_back(): vector is empty
The size of v = 1
v[0] = 99
miniVector: index range error index 1 size = 1
*/
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?