Vector
深浅拷贝
实现原理
自我实现
#include <iostream>
template <typename T>
class SimpleVector {
private:
T* data; // 指向数据的指针
size_t size; // 当前元素个数
size_t capacity; // 容量
// 扩容函数
void resizeIfNeeded() {
if (size >= capacity) {
size_t newCapacity = capacity == 0 ? 1 : 2 * capacity;
T* newData = new T[newCapacity];
for (size_t i = 0; i < size; ++i) {
newData[i] = std::move(data[i]);
}
delete[] data;
data = newData;
capacity = newCapacity;
}
}
public:
// 构造函数
SimpleVector() : data(nullptr), size(0), capacity(0) {}
// 析构函数
~SimpleVector() {
delete[] data;
}
// 尾部添加元素
void push_back(const T& value) {
resizeIfNeeded();
data[size++] = value;
}
// 获取大小
size_t getSize() const {
return size;
}
// 获取容量
size_t getCapacity() const {
return capacity;
}
// 访问元素
T& operator[](size_t index) {
return data[index];
}
// 访问元素(const)
const T& operator[](size_t index) const {
return data[index];
}
};
int main() {
SimpleVector<int> vec;
vec.push_back(1);
vec.push_back(2);
vec.push_back(3);
for (size_t i = 0; i < vec.getSize(); ++i) {
std::cout << vec[i] << " ";
}
std::cout << std::endl;
std::cout << "Size: " << vec.getSize() << std::endl;
std::cout << "Capacity: " << vec.getCapacity() << std::endl;
SimpleVector<char> vChar;
vChar.push_back('m');
std::cout<<vChar[0]<<std::endl;
return 0;
}
List
自我实现
template <typename T>
class List {
private:
Node<T>* head;
Node<T>* tail;
size_t size;
public:
// 构造函数
List() : head(nullptr), tail(nullptr), size(0) {}
// 析构函数
~List() {
clear();
}
// 清空链表
void clear() {
while (head != nullptr) {
Node<T>* temp = head;
head = head->next;
delete temp;
}
tail = nullptr;
size = 0;
}
// 获取大小
size_t getSize() const {
return size;
}
// 插入元素
void insert(size_t position, const T& value) {
if (position > size) {
throw std::out_of_range("Position out of range");
}
Node<T>* newNode = new Node<T>(value);
if (position == 0) {
// 插入到头部
newNode->next = head;
if (head != nullptr) {
head->prev = newNode;
}
head = newNode;
if (tail == nullptr) {
tail = newNode;
}
} else if (position == size) {
// 插入到尾部
newNode->prev = tail;
if (tail != nullptr) {
tail->next = newNode;
}
tail = newNode;
if (head == nullptr) {
head = newNode;
}
} else {
// 插入到中间
Node<T>* current = head;
for (size_t i = 0; i < position; ++i) {
current = current->next;
}
newNode->next = current;
newNode->prev = current->prev;
current->prev->next = newNode;
current->prev = newNode;
}
++size;
}
// 打印链表
void print() const {
Node<T>* current = head;
while (current != nullptr) {
std::cout << current->data << " ";
current = current->next;
}
std::cout << std::endl;
}
};
细节问题分析
List和vector的区别
迭代器失效
数组或链表实现队列结构
#include <iostream>
#include <stdexcept>
template <typename T>
class ArrayQueue {
private:
T* data;
size_t capacity;
int front;
int rear;
size_t count;
public:
// 构造函数
ArrayQueue(size_t capacity) : capacity(capacity), front(0), rear(-1), count(0) {
data = new T[capacity];
}
// 析构函数
~ArrayQueue() {
delete[] data;
}
// 入队操作
void enqueue(const T& value) {
if (count == capacity) {
throw std::overflow_error("Queue overflow");
}
rear = (rear + 1) % capacity;
data[rear] = value;
++count;
}
// 出队操作
void dequeue() {
if (count == 0) {
throw std::underflow_error("Queue underflow");
}
front = (front + 1) % capacity;
--count;
}
// 获取队列前端元素
T& peek() {
if (count == 0) {
throw std::underflow_error("Queue is empty");
}
return data[front];
}
// 判断队列是否为空
bool isEmpty() const {
return count == 0;
}
// 获取队列的大小
size_t size() const {
return count;
}
};
// 测试代码
int main() {
ArrayQueue<int> queue(10);
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
std::cout << "Front element: " << queue.peek() << std::endl;
queue.dequeue();
std::cout << "Front element after dequeue: " << queue.peek() << std::endl;
std::cout << "Queue size: " << queue.size() << std::endl;
return 0;
}
#include <iostream>
#include <stdexcept>
template <typename T>
struct Node {
T data;
Node* next;
Node(const T& value) : data(value), next(nullptr) {}
};
template <typename T>
class LinkedListQueue {
private:
Node<T>* front;
Node<T>* rear;
size_t count;
public:
// 构造函数
LinkedListQueue() : front(nullptr), rear(nullptr), count(0) {}
// 析构函数
~LinkedListQueue() {
while (front != nullptr) {
dequeue();
}
}
// 入队操作
void enqueue(const T& value) {
Node<T>* newNode = new Node<T>(value);
if (rear) {
rear->next = newNode;
} else {
front = newNode;
}
rear = newNode;
++count;
}
// 出队操作
void dequeue() {
if (front == nullptr) {
throw std::underflow_error("Queue underflow");
}
Node<T>* temp = front;
front = front->next;
if (front == nullptr) {
rear = nullptr;
}
delete temp;
--count;
}
// 获取队列前端元素
T& peek() {
if (front == nullptr) {
throw std::underflow_error("Queue is empty");
}
return front->data;
}
// 判断队列是否为空
bool isEmpty() const {
return front == nullptr;
}
// 获取队列的大小
size_t size() const {
return count;
}
};
// 测试代码
int main() {
LinkedListQueue<int> queue;
queue.enqueue(1);
queue.enqueue(2);
queue.enqueue(3);
std::cout << "Front element: " << queue.peek() << std::endl;
queue.dequeue();
std::cout << "Front element after dequeue: " << queue.peek() << std::endl;
std::cout << "Queue size: " << queue.size() << std::endl;
return 0;
}
数组或链表实现栈结构
#include <iostream>
#include <stdexcept>
template <typename T>
struct Node {
T data;
Node* next;
Node(const T& value) : data(value), next(nullptr) {}
};
template <typename T>
class LinkedListStack {
private:
Node<T>* top;
size_t count;
public:
// 构造函数
LinkedListStack() : top(nullptr), count(0) {}
// 析构函数
~LinkedListStack() {
while (top != nullptr) {
pop();
}
}
// 入栈操作
void push(const T& value) {
Node<T>* newNode = new Node<T>(value);
newNode->next = top;
top = newNode;
++count;
}
// 出栈操作
void pop() {
if (top == nullptr) {
throw std::underflow_error("Stack underflow");
}
Node<T>* temp = top;
top = top->next;
delete temp;
--count;
}
// 获取栈顶元素
T& peek() {
if (top == nullptr) {
throw std::underflow_error("Stack is empty");
}
return top->data;
}
// 判断栈是否为空
bool isEmpty() const {
return top == nullptr;
}
// 获取栈的大小
size_t size() const {
return count;
}
};
// 测试代码
int main() {
LinkedListStack<int> stack;
stack.push(1);
stack.push(2);
stack.push(3);
std::cout << "Top element: " << stack.peek() << std::endl;
stack.pop();
std::cout << "Top element after pop: " << stack.peek() << std::endl;
std::cout << "Stack size: " << stack.size() << std::endl;
return 0;
}
#include <iostream>
#include <stdexcept>
template <typename T>
class ArrayStack {
private:
T* data;
size_t capacity;
int top;
public:
// 构造函数
ArrayStack(size_t capacity) : capacity(capacity), top(-1) {
data = new T[capacity];
}
// 析构函数
~ArrayStack() {
delete[] data;
}
// 入栈操作
void push(const T& value) {
if (top == capacity - 1) {
throw std::overflow_error("Stack overflow");
}
data[++top] = value;
}
// 出栈操作
void pop() {
if (top == -1) {
throw std::underflow_error("Stack underflow");
}
--top;
}
// 获取栈顶元素
T& peek() {
if (top == -1) {
throw std::underflow_error("Stack is empty");
}
return data[top];
}
// 判断栈是否为空
bool isEmpty() const {
return top == -1;
}
// 获取栈的大小
size_t size() const {
return top + 1;
}
};
// 测试代码
int main() {
ArrayStack<int> stack(10);
stack.push(1);
stack.push(2);
stack.push(3);
std::cout << "Top element: " << stack.peek() << std::endl;
stack.pop();
std::cout << "Top element after pop: " << stack.peek() << std::endl;
std::cout << "Stack size: " << stack.size() << std::endl;
return 0;
}