23.03.21 学习
ChatGPT真好用!!!!!
String类
#include <iostream>
#include <cstring>
using namespace std;
/* string类
* 构造函数:
* string(const char* s);
* string(const string& str); //拷贝构造,使用一个string对象初始化另一个string对象
* string(string &&str); //移动构造,使用一个string对象初始化另一个string对象并删掉原对象指针
* 析构函数:
* ~string();
* 重载运算符:
* string & operator=(const string &str); // 拷贝赋值运算符=,已有对象再赋值
* string & operator=(string & str) // 移动赋值运算符=,已有对象再赋值,区别类似
* string operator+(const string &str) const; // 重新+,返回新字符串
* 成员函数:
* size_t size() const; //获取字符串长度
* 输入输出:
* friend istream& operator>>(istream &is, string &str);
* friend ostream& operator<<(ostream &os, string &str);
*/
class String {
public:
String() {
m_size = 0;
m_data = new char[1];
*m_data = '\0';
}
String(const char* str) { //通用构造,使用字符串s初始化
m_size = strlen(str);
m_data = new char[m_size + 1];
strcpy(m_data, str);
}
String(const String& str) { //拷贝构造,使用一个string对象初始化另一个string对象
m_size = str.m_size;
m_data = new char[m_size + 1];
strcpy(m_data, str.m_data);
}
String(String&& str) : m_data(str.m_data), m_size(str.m_size) { //移动构造,使用一个string对象初始化另一个string对象并删掉原对象指针
str.m_data = nullptr;
str.m_size = 0;
}
String& operator=(const String& str) { // 拷贝赋值运算符=,已有对象再赋值
if (this != &str) {
delete[] m_data;
m_size = str.m_size;
m_data = new char[m_size + 1];
strcpy(m_data, str.m_data);
}
return *this;
}
String& operator=(String&& str) { // 移动赋值运算符=,已有对象再赋值,区别类似
if (this != &str) {
delete[] m_data;
m_size = str.m_size;
m_data = str.m_data;
str.m_data = nullptr;
str.m_size = 0;
}
return *this;
}
String operator+(const String& str) const { // 重新+,返回新字符串
String newStr;
newStr.m_size = m_size + str.m_size;
newStr.m_data = new char[newStr.m_size + 1];
strcpy(newStr.m_data, m_data);
strcat(newStr.m_data, str.m_data);
return newStr;
}
size_t size() const {
return m_size;
}
friend istream& operator>>(istream& is, String& str) { // 重载>>,先申请一块足够大的内存
char tem[1000]; //简单的申请一块内存
is >> tem;
str.m_size = strlen(tem);
str.m_data = new char[str.m_size + 1];
strcpy(str.m_data, tem);
return is;
}
friend ostream& operator<<(ostream& os, String& str) { // 重载<<
os << str.m_data;
return os;
}
~String() {
if (m_data)
delete[] m_data;
}
private:
char* m_data;
size_t m_size;
};
int main()
{
String s1 = "123";
cout << s1 << endl;
String s2(move(s1));
cout << s2 << endl;
return 0;
}
Vector类
#include <iostream>
using namespace std;
template<typename T>
class Vector {
private:
T* data;
int size;
int capacity;
public:
typedef T* iterator;
// 构造函数
Vector() {
size = 0;
capacity = 4;
data = new T[capacity];
}
// 复制构造函数
Vector(const Vector& other) {
size = other.size;
capacity = other.capacity;
data = new T[capacity];
for (int i = 0; i < size; i++) {
data[i] = other.data[i];
}
}
// 析构函数
~Vector() {
delete[] data;
}
// 在尾部添加元素
void push_back(T val) {
if (size == capacity) {
reserve(capacity * 2);
}
data[size++] = val;
}
// 删除尾部元素
void pop_back() {
if (size > 0) {
size--;
}
}
// 重置大小
void resize(int n) {
if (n > capacity) {
reserve(n);
}
size = n;
}
// 重置容量
void reserve(int n) {
if (n > capacity) {
T* newData = new T[n];
for (int i = 0; i < size; i++) {
newData[i] = data[i];
}
delete[] data;
data = newData;
capacity = n;
}
}
// 获取元素个数
int getSize() {
return size;
}
// 获取元素容量
int getCapacity() {
return capacity;
}
// 重载下标运算符
T& operator[](int i) {
return data[i];
}
// 迭代器
iterator begin() { return data; }
iterator end() { return data + size; }
};
int main() {
Vector<int> v;
/*
测试代码
*/
return 0;
}