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

相关推荐
jf加菲猫14 分钟前
条款21:优先选用std::make_unique、std::make_shared,而非直接new
开发语言·c++
scx2013100420 分钟前
20251019状压DP总结
c++
消失的旧时光-194326 分钟前
Kotlin 高阶函数在回调设计中的最佳实践
android·开发语言·kotlin
LucianaiB1 小时前
掌握 Rust:从内存安全到高性能服务的完整技术图谱
开发语言·安全·rust
m0_748240251 小时前
C++ 游戏开发示例:简单的贪吃蛇游戏
开发语言·c++·游戏
Summer_Uncle1 小时前
【C++学习】指针
c++·学习
兰亭妙微2 小时前
2026年UX/UI五大趋势:AI、AR与包容性设计将重新定义用户体验
开发语言·ui·1024程序员节·界面设计·设计趋势
蜗牛沐雨2 小时前
详解C++中的字符串流
c++·1024程序员节
蜗牛沐雨2 小时前
详解C++中的流
c++·1024程序员节
懒羊羊不懒@2 小时前
Java—枚举类
java·开发语言·1024程序员节