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

相关推荐
CoderYanger7 分钟前
一轮复习——F.动态规划模型总结(入门篇)
java·开发语言·数据结构·算法·leetcode·职场和发展·动态规划
Frank_refuel26 分钟前
【C++】智能指针
开发语言·c++
泡海椒35 分钟前
JQuick-Excel VALIDATION 实战:用作用范围约束 Excel 导入校验
开发语言·python·excel
xuxigifxfh1 小时前
HJ11 数字颠倒
java·开发语言·华为机考
一只旭宝1 小时前
数据结构与算法复习手册:从写过,到讲清楚
开发语言·数据结构·c++·算法
百度一下吧1 小时前
断网后 WebView 加载单页面应用:页面跳转 JS 加载失败的处理方案
开发语言·javascript·ecmascript
十五年专注C++开发1 小时前
std::filesystem::exists 在有的win10系统抛异常原因分析
c++·文件系统·filesystem
神仙别闹2 小时前
基于C语言开发的捕捉流星小游戏
c语言·开发语言
程序员Sunday2 小时前
Promise.all、allSettled、race、any 怎么选,失败后其他请求会怎样
开发语言·前端·javascript
hetao17338372 小时前
2026-10-07 hetao1733837 的刷题记录
c++·算法