C++作业

1、思维导图:

2、模板类的课上练习

cpp 复制代码
#include <iostream>
#include <stdexcept>
using namespace std;
template <typename T, int MAXSIZE>
class Stack {
private:
    T data[MAXSIZE];
    int top;
public:
    Stack() : top(-1) {}
    bool isEmpty() const {
        return top == -1;
    }
    void push(const T& x) {
        if (top == MAXSIZE - 1) {
            cout<<"stack full"<<endl;
        }
        data[++top] = x;
    }
    void pop() {
        if (isEmpty()) {
           cout<<"stack empty"<<endl;
        }
        top--;
    }
};
int main() {
    Stack<int, 10> s;
    try {
        s.push(1);
        s.push(2);
        s.push(3);
    } catch (const exception& e) {
        cout << "Error: " << e.what() << endl;
    }
    return 0;
}

3、异常处理的代码重新写一遍

相关推荐
niucloud-admin15 小时前
NIUCLOUD PHP SAAS上门家政使用文档--服务订单:订单列表
开发语言·php
D e re k ZH15 小时前
P5025 [SNOI2017] 炸弹
c++·算法
怪我冷i15 小时前
Rust 的impl语法详解
开发语言·后端·rust
wuminyu15 小时前
Java字节码学习总结:字节码如何加载、解析和执行
java·linux·c语言·jvm·c++
qq_4069810315 小时前
c++的decltype是什么
开发语言·c++
坤坤子吖16 小时前
算法学习——单调队列
开发语言·c++·笔记·学习·算法
我就是全世界16 小时前
OpenCode Go 套餐划算吗:先看你用哪个模型
开发语言·后端·golang
螺丝钉tina16 小时前
[Playwright] [02]常用命令与调试:Debug UI、Mode和 Trace Viewer
开发语言·javascript·测试工具·ui·自动化
杜 硕16 小时前
从零开始的C++生活——第六讲——01
开发语言·c++·生活
淡漠的蓝精灵16 小时前
web.py:一个简单到极致的 Python Web 框架
开发语言·前端·python·其他