C++11中智能指针以及标准模板库 My_string My_stack

news/2024/10/5 4:51:11/

在这里插入图片描述
My_string.h

#ifndef MY_STRING_H
#define MY_STRING_H#include <iostream>
#include <cstring>
#include <stdexcept>using namespace std;template<typename T>
class My_string {
private:T *ptr;         // 指向字符数组的指针int size;       // 字符串的最大容量int len;        // 字符串当前长度public:My_string();  // 无参构造My_string(const T* src);  // 有参构造My_string(int num, T value);  // 重复构造My_string(const My_string &other);  // 拷贝构造My_string &operator=(const My_string &other);  // 拷贝赋值~My_string();  // 析构函数void show() const;  // 显示内容void isempty() const;  // 判空void isfull() const;  // 判满void push_back(T value);  // 尾插void pop_back();  // 尾删T &at(int index);  // at函数实现void clear();  // 清空函数T* data();  // 返回C风格字符串int get_length() const;  // 返回实际长度int get_size() const;  // 返回当前最大容量void resize();  // 二倍扩容My_string operator+(const My_string &R) const;  // 自定义运算符重载 +T operator[](int n) const;  // 自定义运算符重载 []// 重载比较运算符bool operator>(const My_string &R) const;bool operator<(const My_string &R) const;bool operator<=(const My_string &R) const;bool operator>=(const My_string &R) const;bool operator==(const My_string &R) const;bool operator!=(const My_string &R) const;// 重载 += 运算符My_string& operator+=(const My_string &R);// 重载输出运算符 <<friend ostream& operator<<(ostream &L, const My_string &R) {L << R.ptr;return L;}
};#include "My_string.cpp"  // 包含实现文件#endif // MY_STRING_H

My_string.cpp

#include "My_string.h"// 无参构造
template<typename T>
My_string<T>::My_string() : size(20), len(0) {cout << "****************无参构造***********" << endl;ptr = new T[size];ptr[0] = '\0';  // 表示串为空串
}// 有参构造
template<typename T>
My_string<T>::My_string(const T* src) {cout << "****************一个参数有参构造***********" << endl;len = strlen(src);size = len + 1;ptr = new T[size];strcpy(ptr, src);
}// 初始化字符重复构造
template<typename T>
My_string<T>::My_string(int num, T value) {cout << "****************两个参数有参构造***********" << endl;len = num;size = len + 1;ptr = new T[size];for (int i = 0; i < num; i++) {ptr[i] = value;}ptr[num] = '\0';
}// 拷贝构造
template<typename T>
My_string<T>::My_string(const My_string &other) {cout << "****************拷贝构造***********" << endl;len = other.len;size = other.size;ptr = new T[size];strcpy(ptr, other.ptr);
}// 拷贝赋值
template<typename T>
My_string<T>& My_string<T>::operator=(const My_string &other) {cout << "****************拷贝赋值***********" << endl;if (this == &other) {return *this;  // 直接返回当前对象}delete[] ptr;len = other.len;size = other.size;ptr = new T[size];strcpy(ptr, other.ptr);return *this;
}// 析构函数
template<typename T>
My_string<T>::~My_string() {cout << "****************析构函数***********" << endl;delete[] ptr;
}// 显示内容
template<typename T>
void My_string<T>::show() const {cout << "内容: " << ptr << ", 长度: " << len << ", 容量: " << size << endl;
}// 判空
template<typename T>
void My_string<T>::isempty() const {if (len == 0) {cout << "字符串为空" << endl;}
}// 判满
template<typename T>
void My_string<T>::isfull() const {if (len >= size) {cout << "字符串满" << endl;resize();cout << "重新分配空间" << endl;} else {cout << "字符串未满" << endl;}
}// 尾插
template<typename T>
void My_string<T>::push_back(T value) {isfull();ptr[len] = value;len++;ptr[len] = '\0';
}// 尾删
template<typename T>
void My_string<T>::pop_back() {if (len > 0) {len--;ptr[len] = '\0';}
}// at函数实现
template<typename T>
T& My_string<T>::at(int index) {if (index < 0 || index >= len) {throw out_of_range("Index out of range");}return ptr[index];
}// 清空函数
template<typename T>
void My_string<T>::clear() {ptr[0] = '\0';len = 0;
}// 返回C风格字符串
template<typename T>
T* My_string<T>::data() {return ptr;
}// 返回实际长度
template<typename T>
int My_string<T>::get_length() const {return len;
}// 返回当前最大容量
template<typename T>
int My_string<T>::get_size() const {return size;
}// 君子函数:二倍扩容
template<typename T>
void My_string<T>::resize() {size *= 2;T* new_ptr = new T[size];strcpy(new_ptr, ptr);delete[] ptr;ptr = new_ptr;
}// 自定义运算符重载 +
template<typename T>
My_string<T> My_string<T>::operator+(const My_string &R) const {My_string temp;while (this->len + R.len > temp.size) {temp.resize();}strcpy(temp.ptr, this->ptr);strcat(temp.ptr, R.ptr);temp.len = this->len + R.len;return temp;
}// 自定义运算符重载 []
template<typename T>
T My_string<T>::operator[](int n) const {return ptr[n];
}// 重载大于运算符
template<typename T>
bool My_string<T>::operator>(const My_string &R) const {return strcmp(this->ptr, R.ptr) > 0;
}// 重载小于运算符
template<typename T>
bool My_string<T>::operator<(const My_string &R) const {return strcmp(this->ptr, R.ptr) < 0;
}// 重载小于等于运算符
template<typename T>
bool My_string<T>::operator<=(const My_string &R) const {return strcmp(this->ptr, R.ptr) <= 0;
}// 重载大于等于运算符
template<typename T>
bool My_string<T>::operator>=(const My_string &R) const {return strcmp(this->ptr, R.ptr) >= 0;
}// 重载相等运算符
template<typename T>
bool My_string<T>::operator==(const My_string &R) const {return strcmp(this->ptr, R.ptr) == 0;
}// 重载不等运算符
template<typename T>
bool My_string<T>::operator!=(const My_string &R) const {return strcmp(this->ptr, R.ptr) != 0;
}// 重载 += 运算符
template<typename T>
My_string<T>& My_string<T>::operator+=(const My_string &R) {while (this->len + R.len >= this->size) {this->resize();}strcat(this->ptr, R.ptr);this->len += R.len;return *this;
}

main.cpp

#include "My_string.h"int main() {My_string<char> str1("Hello");My_string<char> str2(" World");My_string<char> str3 = str1 + str2;str3.show();  return 0;
}

栈的模板类

My_stack.h

#ifndef MY_STACK_H
#define MY_STACK_H#include <iostream>
using namespace std;template<typename T>
class My_Stack {
private:T* ptr;         // 动态数组用于存储栈元素int top;        // 栈顶索引int capacity;   // 当前容量void resize();  // 调整栈的大小public:My_Stack();                // 构造函数My_Stack(const My_Stack& other);  // 拷贝构造函数~My_Stack();               // 析构函数void push(T value);        // 入栈操作T pop();                   // 出栈操作T peek();                  // 获取栈顶元素bool isEmpty();            // 判断栈是否为空int size();                // 获取栈的当前大小void swap_t(My_Stack& other); // 交换栈void show();               // 显示栈中的内容My_Stack& operator=(const My_Stack& other);  // 赋值操作符重载
};#include "My_stack.cpp"  // 引入实现文件#endif // MY_STACK_H

My_stack.cpp

#include "My_stack.h"// 构造函数
template<typename T>
My_Stack<T>::My_Stack() : top(-1), capacity(1) {ptr = new T[capacity];  // 初始化容量为1
}// 拷贝构造函数
template<typename T>
My_Stack<T>::My_Stack(const My_Stack& other) : top(other.top), capacity(other.capacity) {ptr = new T[capacity];  // 分配新的内存for (int i = 0; i <= top; ++i) {ptr[i] = other.ptr[i];  // 深拷贝}
}// 析构函数
template<typename T>
My_Stack<T>::~My_Stack() {delete[] ptr;  // 释放动态数组的内存
}// 动态扩容
template<typename T>
void My_Stack<T>::resize() {capacity *= 2;  // 容量翻倍T* new_arr = new T[capacity];  // 创建新的数组for (int i = 0; i <= top; ++i) {new_arr[i] = ptr[i];  // 拷贝原数组元素}delete[] ptr;  // 释放旧数组ptr = new_arr;  // 更新指针
}// 入栈操作
template<typename T>
void My_Stack<T>::push(T value) {if (top >= capacity - 1) {resize();  // 容量不足时进行扩容}ptr[++top] = value;  // 增加栈顶并赋值cout << "入栈: " << value << endl;
}// 出栈操作
template<typename T>
T My_Stack<T>::pop() {if (isEmpty()) {cout << "栈为空!无法出栈。" << endl;return T();  // 返回默认值}T popValue = ptr[top--];  // 返回栈顶值并减少栈顶索引cout << "出栈: " << popValue << endl;return popValue;
}// 获取栈顶元素
template<typename T>
T My_Stack<T>::peek() {if (isEmpty()) {cout << "栈为空!无法查看栈顶元素。" << endl;return T();  // 返回默认值}return ptr[top];  // 返回栈顶值
}// 判断栈是否为空
template<typename T>
bool My_Stack<T>::isEmpty() {return top == -1;  // 栈为空时,栈顶索引为 -1
}// 获取栈的当前大小
template<typename T>
int My_Stack<T>::size() {return top + 1;  // 返回栈中元素的个数
}// 交换栈
template<typename T>
void My_Stack<T>::swap_t(My_Stack& other) {swap(top, other.top);swap(capacity, other.capacity);swap(ptr, other.ptr);  // 交换指针
}// 显示栈中的内容
template<typename T>
void My_Stack<T>::show() {if (isEmpty()) {cout << "栈为空!" << endl;return;}cout << "栈中的元素: ";for (int i = 0; i <= top; ++i) {cout << ptr[i] << " ";  // 打印每个元素}cout << endl;
}// 赋值操作符重载
template<typename T>
My_Stack<T>& My_Stack<T>::operator=(const My_Stack& other) {if (this != &other) {  // 自我赋值检查delete[] ptr;  // 释放旧数组top = other.top;capacity = other.capacity;ptr = new T[capacity];  // 分配新的内存for (int i = 0; i <= top; ++i) {ptr[i] = other.arr[i];  // 深拷贝}}return *this;  // 返回当前对象的引用
}

main.cpp

#include "My_stack.h"int main() {// 创建第一个栈并进行入栈操作My_Stack<int> s1;s1.push(10);s1.push(20);s1.push(30);// 创建第二个栈并进行入栈操作My_Stack<int> s2;s2.push(40);s2.push(50);My_Stack<int> s3 = s2;cout<<"s3:";s3.show();cout <<"交换前:"<< endl;cout<<"s1:";s1.show();cout<<"s2:";s2.show();// 交换s1和s2的内容s1.swap_t(s2);cout << "交换后:" << endl;cout<<"s1:";s1.show();cout<<"s2:";s2.show();// 出栈cout<<"从 s1 弹出元素: "<<s1.pop()<<endl;cout<<"从 s2 弹出元素: "<<s2.pop()<<endl;// 检查栈的大小cout<<"s1 当前大小: "<<s1.size()<<endl;cout<<"s2 当前大小: "<<s2.size()<<endl;// 再次弹出元素s1.pop();s2.pop();cout << "交换后再次弹出后的大小:" << endl;cout << "s1 当前大小: " << s1.size() << endl;cout << "s2 当前大小: " << s2.size() << endl;return 0;
}

http://www.ppmy.cn/news/1534751.html

相关文章

计算机毕业设计Hadoop+Spark知识图谱美团美食推荐系统 美团餐厅推荐系统 美团推荐系统 美食价格预测 美团爬虫 美食数据分析 美食可视化大屏

《HadoopSpark知识图谱美团美食推荐系统》开题报告 一、研究背景与意义 随着互联网技术的快速发展&#xff0c;大数据已成为企业竞争力的关键要素。美团作为国内领先的本地生活服务平台&#xff0c;拥有海量的用户行为数据和丰富的业务场景。然而&#xff0c;面对如此庞大的数…

leetcode练习 路径总和II

给你二叉树的根节点 root 和一个整数目标和 targetSum &#xff0c;找出所有 从根节点到叶子节点 路径总和等于给定目标和的路径。 叶子节点 是指没有子节点的节点。 示例 1&#xff1a; 输入&#xff1a;root [5,4,8,11,null,13,4,7,2,null,null,5,1], targetSum 22 输出&a…

如何从相机的记忆棒(存储卡)中恢复丢失照片

当您意识到不小心从存储卡中删除了照片&#xff0c;或者错误地格式化了相机的记忆棒时&#xff0c;这些是您会大喊的前两个词。这是一种常见的情况&#xff0c;每个人在他们的一生中都会面临它。幸运的是&#xff0c;有一些方法可以从相机的 RAW 记忆棒&#xff08;存储卡&…

pWnOS2.0 靶机渗透( cms 渗透,php+mysql 网站渗透,密码碰撞)

pWnOS2.0 靶机渗透( ) 靶机介绍 vulnhub 靶机 本地搭建 由于靶机特性&#xff0c;靶机网卡位nat模式扫不到&#xff0c;原来需要改 nat 的地址 参考方法 https://blog.csdn.net/Bossfrank/article/details/131415257 作者主页 https://blog.csdn.net/Bossfrank?typeblog P…

将视频改成代码滚动

本文章就来讲讲如何将视频转换成代码滚动&#xff0c;也就是这种模式&#xff1a; 本文章就来详细的教大家如何制作达到这种效果吧&#xff01; &#xff08;注&#xff1a;我记得一些python库也可以轻松达到这些效果&#xff0c;但我一时半伙想不起来了&#xff0c;所以这里用…

【漏洞复现】孚盟云oa AjaxSendDingdingMessage接口 存在sql注入漏洞

》》》产品描述《《《 孚盟与阿里强强联手将最受青睐的经典C系列产品打造成全新的孚盟云产品&#xff0c;让用户可以用云模式实现信息化管理&#xff0c;让用户的异地办公更加流畅&#xff0c;大大降低中小企业在信息化上成本&#xff0c;用最小的投入享受大型企业级别的信息化…

算法笔记(七)——哈希表

文章目录 两数之和判定是否互为字符重排存在重复元素存在重复元素 II字母异位词分组 哈希表&#xff1a;一种存储数据的容器&#xff1b; 可以快速查找某个元素&#xff0c;时间复杂度O(1)&#xff1b; 当频繁查找某一个数时&#xff0c;我们可以使用哈希表 创建一个容器&#…

AAC-Fe³⁺水凝胶,兼具拉伸性与导电性,还有自修复和4D打印能力

大家好&#xff01;今天我们来了解一种用于可拉伸电子产品的创新材料——自修复和4D打印水凝胶——《Self‐Healable and 4D Printable Hydrogel for Stretchable Electronics》发表于《Advanced Science》。在科技发展中&#xff0c;可拉伸电子产品需求大增&#xff0c;但现有…