C++学习 2024.9.4

用模板封装栈,并且栈内可存放任意数据类型

cpp 复制代码
#include <iostream>  
#include <vector>  
  
using namespace std;  
  
template <typename T>  
class Stack {  
private:  
    vector<T> stack;  // 使用vector存储元素,T是模板参数,表示元素的类型  
  
public:  
    // 默认构造  
    Stack() = default;  
  
    // 判空  
    bool empty() const {  
        return stack.empty();  
    }  
  
    // 入栈  
    void push(const T& element) {  
        stack.push_back(element);  
    }  
  
    // 出栈  
    void pop() {  
        if (empty()) {  
            cout << "栈为空,出栈失败" << endl;  
        } else {  
            stack.pop_back();  
        }  
    }  
  
    // 栈内元素个数  
    size_t size() const {  
        return stack.size();  
    }  
  
    // 访问栈顶元素  
    T top() const {  
        if (!empty()) {  
            return stack.back();  
        } else {  
            throw runtime_error("栈为空,无法访问栈顶元素");  
        }  
    }  
  
    // 展示函数  
    void show() const {  
        cout << "栈内元素为: ";  
        for (const T& elem : stack) {  
            cout << elem << " ";  
        }  
        cout << endl;  
    }  
};  
  
int main() {  
    // 创建一个存放整数的栈  
    Stack<int> intStack;  
    intStack.push(5);  
    intStack.push(2);  
    intStack.push(0);  
    intStack.show();             // 输出: 栈内元素为: 5 2 0  
    cout << "栈顶元素为: " << intStack.top() << endl;  
  
    // 创建一个存放字符串的栈  
    Stack<string> stringStack;  
    stringStack.push("Hello");  
    stringStack.push("World");  
    stringStack.show();          // 输出: 栈内元素为: Hello World  
    cout << "栈顶元素为: " << stringStack.top() << endl;  
  
    // 其他操作...  
      
    return 0;  
}

同样的形式封装队列

cpp 复制代码
#include <iostream>  
#include <vector>  
  
template <typename T>  
class Queue  
{  
private:  
    std::vector<T> queue;  // 使用vector作为底层存储,存储任意类型T  
  
public:  
    // 默认构造  
    Queue() = default;  
  
    // 判空  
    bool empty() const  
    {  
        return queue.empty();  
    }  
  
    // 访问队头元素  
    void at_front() const  
    {  
        if (empty())  
        {  
            std::cout << "队列为空" << std::endl;  
        }  
        else  
        {  
            std::cout << "队头元素为:" << queue.front() << std::endl;  
        }  
    }  
  
    // 访问队尾元素  
    void at_last() const  
    {  
        if (empty())  
        {  
            std::cout << "队列为空" << std::endl;  
        }  
        else  
        {  
            std::cout << "队尾元素为:" << queue.back() << std::endl;  
        }  
    }  
  
    // 队列中元素个数  
    size_t size() const  
    {  
        return queue.size();  
    }  
  
    // 向队尾插入元素  
    void push_in(const T& e)  
    {  
        queue.push_back(e);  
    }  
  
    // 删除首个元素  
    void pop()  
    {  
        if (empty())  
        {  
            std::cout << "队列为空" << std::endl;  
        }  
        else  
        {  
            queue.erase(queue.begin());  
        }  
    }  
  
    // 展示函数  
    void show() const  
    {  
        if (empty())  
        {  
            std::cout << "队列为空" << std::endl;  
        }  
        else  
        {  
            std::cout << "队列中元素为:";  
            for (const T& elem : queue)  
            {  
                std::cout << " " << elem;  
            }  
            std::cout << std::endl;  
        }  
    }  
};  
  
int main()  
{  
    // 创建一个存储整数的队列  
    Queue<int> intQueue;  
    intQueue.push_in(1);  
    intQueue.push_in(3);  
    intQueue.push_in(1);  
    intQueue.push_in(4);  
    intQueue.show();       // 1 3 1 4  
    std::cout << "队列中元素个数为:" << intQueue.size() << std::endl;   // 4  
    intQueue.pop();  
    std::cout << "执行一次pop函数后" << std::endl;  
    intQueue.show();  // 3 1 4  
    intQueue.at_front();  
    intQueue.at_last();    // 访问队尾元素应显示4  
  
    // 创建一个存储字符串的队列  
    Queue<std::string> stringQueue;  
    stringQueue.push_in("hello");  
    stringQueue.push_in("world");  
    stringQueue.show();       // hello world  
    stringQueue.at_front();   // 队头元素为: hello  
    stringQueue.at_last();    // 队尾元素为: world  
  
    return 0;  
}
相关推荐
岁忧1 小时前
(LeetCode 面试经典 150 题 ) 11. 盛最多水的容器 (贪心+双指针)
java·c++·算法·leetcode·面试·go
chao_7891 小时前
二分查找篇——搜索旋转排序数组【LeetCode】两次二分查找
开发语言·数据结构·python·算法·leetcode
数字芯片实验室1 小时前
分享一个可以学习正则表达式的网址:Pythex.org
学习·正则表达式
陈洪奇1 小时前
注册中心学习笔记整理
笔记·学习
光影少年1 小时前
从前端转go开发的学习路线
前端·学习·golang
秋说3 小时前
【PTA数据结构 | C语言版】一元多项式求导
c语言·数据结构·算法
Maybyy3 小时前
力扣61.旋转链表
算法·leetcode·链表
卡卡卡卡罗特5 小时前
每日mysql
数据结构·算法
chao_7896 小时前
二分查找篇——搜索旋转排序数组【LeetCode】一次二分查找
数据结构·python·算法·leetcode·二分查找
蜉蝣之翼❉6 小时前
CRT 不同会导致 fopen 地址不同
c++·mfc