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

相关推荐
纵有疾風起9 小时前
C++——多态
开发语言·c++·经验分享·面试·开源
冯诺依曼的锦鲤9 小时前
算法练习:差分
c++·学习·算法
Mr_WangAndy10 小时前
现代C++模板与泛型编程_第4章_remove_all_sequence,integer_sequence,is_union
c++·c++40周年·c++标准库用法
氵文大师10 小时前
A机通过 python -m http.server 下载B机的文件
linux·开发语言·python·http
封奚泽优10 小时前
下降算法(Python实现)
开发语言·python·算法
im_AMBER10 小时前
算法笔记 16 二分搜索算法
c++·笔记·学习·算法
笃行客从不躺平10 小时前
遇到大SQL怎么处理
java·开发语言·数据库·sql
郝学胜-神的一滴10 小时前
Python中常见的内置类型
开发语言·python·程序人生·个人开发
g***B73811 小时前
Kotlin协程在Android中的使用
android·开发语言·kotlin
火白学安全11 小时前
《Python红队攻防零基础脚本编写:进阶篇(一)》
开发语言·python·安全·web安全·网络安全·系统安全