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

相关推荐
字节渡客1 分钟前
PHP项目复盘:foreach 引用残留造成的诡异数据错乱
开发语言·php
YYYing.3 分钟前
【设计模式系列 (八) 】组合模式
c++·后端·设计模式·组合模式·c/c++
我就是不信4 分钟前
深入理解 Python 虚拟机:字典(dict)的优化
开发语言·python·哈希算法
艾莉丝努力练剑10 分钟前
【QT】系统相关:多线程QThread基础
开发语言·网络·c++·qt·学习·大模型
l1t15 分钟前
DeepSeek总结的PIVCO-Huffman编码性能优化
数据结构·c++·算法
ysu_031418 分钟前
uv实战指南-从Python版本虚拟环境到pyproject依赖管理
开发语言·python·pip·uv·虚拟环境·包管理·依赖管理
王维同学44 分钟前
ETW监控-KswordARK功能展示
c++·windows·安全
東隅已逝,桑榆非晚1 小时前
c++模板进阶
开发语言·c++·笔记·学习
Cc.Y5 小时前
Java零基础入门:字符串深度掌握——从基础API到StringBuilder性能优化
java·开发语言·性能优化
java资料站9 小时前
十一、评估测试
开发语言·人工智能·python