欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 房产 > 建筑 > --杂项2--

--杂项2--

2024/10/25 21:20:21 来源:https://blog.csdn.net/qq_64966970/article/details/142601642  浏览:    关键词:--杂项2--

将之前实现的顺序表、栈、队列都更改成模板类

#include <iostream>
#include <string.h>
using namespace std;template <typename T>
class Stack {
private:T* a;int top;int size1;public:Stack(int c) : a(new T[c]), top(-1), size1(c) {}~Stack() { delete[] a; }T front() const {if (empty()) {throw out_of_range("Stack is empty");}return a[top];}T back() const {if (empty()) {throw out_of_range("Stack is empty");}return a[0];}bool empty() const {return top == -1;}int size() const {return top + 1;}void push(const T& e) {if (top == size1 - 1) {throw overflow_error("Stack is full");}a[++top] = e;}void pop() {if (empty()) {throw underflow_error("Stack underflow");}--top;}void Swap(int n, int m) {if (n < 1 || n > size() || m < 1 || m > size()) {throw out_of_range("Invalid indices for swap");}swap(a[n-1], a[m-1]);}void print() const {cout << "Stack: ";for (int i = top; i >= 0; --i) {cout << a[i] << " ";}cout << std::endl;}
};int main() {try {Stack<int> s1(15);cout << "Is empty: " << (s1.empty() ? "Yes" : "No") << endl;s1.push(5);s1.push(2);s1.push(9);s1.push(1);s1.print();cout << "Size: " << s1.size() << endl;cout << "Front: " << s1.front() << endl;cout << "Back: " << s1.back() <<endl;s1.pop();cout << "After pop: ";s1.print();s1.Swap(1, 3);cout << "After swap(1,3): ";s1.print();// 测试其他类型Stack<string> s2(5);s2.push("World");s2.push("Hello");s2.print();}catch (const exception& e) {cerr << "Error: " << e.what() << endl;}return 0;
}

#include <iostream>
#include <string.h>
using namespace std;
template <typename T>
class Queue {
private:T* a;int cap;int size1;int front1;int rear;public:Queue(int c) : a(new T[c]), cap(c), size1(0), front1(0), rear(-1) {}~Queue() { delete[] a; }T front() const {if (empty()) {throw out_of_range("Queue is empty");}return a[front1];}T back() const {if (empty()) {throw out_of_range("Queue is empty");}return a[rear];}bool empty() const {return size1 == 0;}bool full() const {return size1 == cap;}int size() const {return size1;}void push(const T& e) {if (full()) {throw std::overflow_error("Queue is full");}rear = (rear + 1) % cap;a[rear] = e;size1++;}T pop() {if (empty()) {throw std::underflow_error("Queue is empty");}T e = a[front1];front1 = (front1 + 1) % cap;size1--;return e;}void print() const {if (empty()) {cout << "Queue is empty" <<endl;return;}cout << "Queue: ";int count = 0;int index = front1;while (count < size1) {cout << a[index] << " ";index = (index + 1) % cap;count++;}cout << endl;}
};int main() {try {Queue<int> q1(5);cout << "Is empty: " << (q1.empty() ? "Yes" : "No") <<endl;q1.push(5);q1.push(2);q1.push(9);q1.push(1);q1.print();cout << "Size: " << q1.size() << endl;cout << "Front: " << q1.front() << endl;cout << "Back: " << q1.back() << endl;q1.pop();cout << "After pop: ";q1.print();// 测试其他类型Queue<string> q2(3);q2.push("Hello");q2.push("World");q2.print();// 测试异常q2.push("!");try {q2.push("Overflow");} catch (const std::exception& e) {cout << "Caught exception: " << e.what() << endl;}}catch (const std::exception& e) {cerr << "Error: " << e.what() << endl;}return 0;
}

手动将unique_ptr类功能实现出来

template <typename T,typename D=default_delete<T>>
class unique_ptr
{
private:T * ptr;
public:explicit unique_ptr(T*p=nullptr):ptr(p){}~unique_ptr()noexcept{};T& operator *()const{return *ptr;}T* operator->()const{return ptr;}unique_ptr(const unique_ptr &)=delete;unique_ptr& operator=(const unique_ptr &)=delete;unique_ptr(unique_ptr && other) noexcept:ptr(other.ptr){other.ptr=nullptr;}unique_ptr& operator =(unique_ptr &&other)noexcept{if(this!=&other){reset(other.ptr);other.ptr=nullptr;}return *this;}T * get()const{return ptr;}
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com