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、异常处理的代码重新写一遍

相关推荐
ThornArmor2 分钟前
奔腾的呼吸:被驱逐者的灵感
开发语言·程序人生·架构
软件黑马王子12 分钟前
12.缓存池优化:对象上限
开发语言·前端框架·c#
阿里嘎多学长13 分钟前
2026-09-07 GitHub 热点项目精选
开发语言·程序员·github·代码托管
csdn_aspnet13 分钟前
C++ 未排序数组中第 k 个最小/最大元素 | 最坏情况下的线性时间
数据结构·c++·算法
流浪00132 分钟前
大模型技术全景(六):AI 的“逐字接龙“——Token 与自回归生成背后的真相
开发语言·人工智能·llm
Raas1001 小时前
AI网关和OpenRouter区别?MAI Gateway(魔芋企业级AI网关)企业级方案对比指南
大数据·开发语言·人工智能·gateway·php·ai网关·mai gateway
点心的游戏开发世界9 小时前
GDScript 入门笔记(十一):Lambda 与匿名函数
开发语言·笔记·游戏引擎·godot
RuoZoe10 小时前
从 2026 年 3 月 1 日开源,到 26.10.9:Jalium UI 半年时间到底走了多远?
c语言·c++
事圆则缓11 小时前
Kotlin 协程完全指南
开发语言·kotlin
鱼子星_11 小时前
【C++】继承和多态(上)
c++·笔记