C++ 实现 Vector 类:功能详解及代码示例
使用 C++ 语言实现 Vector 类
本示例将展示如何使用 C++ 语言实现一个基本的 Vector 类,该类包含了以下功能:
- 构造函数:创建 Vector 对象。
- 析构函数:销毁 Vector 对象。
- 复制构造函数:创建 Vector 对象的副本。
capacity():返回 Vector 当前存储单元数。size():返回 Vector 中存储的元素个数。empty():当 Vector 中不包含元素时返回 true。reserve():增加 Vector 的容量至指定大小。push_back(value):将元素value添加到 Vector 尾部。pop_back():删除 Vector 的最后一个元素。front():返回 Vector 中第一个元素的引用。back():返回 Vector 中最后一个元素的引用。operator[](int i):返回 Vector 中下标为i的元素,不进行越界检查。at(int i):返回 Vector 中下标为i的元素,进行越界检查。operator=(const Vector& other):将other的副本赋值给当前 Vector 对象。swap(Vector& other):交换当前 Vector 对象和other的内容。operator==(const Vector& other):当两个 Vector 的值和顺序都相同时返回 true。operator<(const Vector& other):当当前 Vector 在字典顺序上小于other时返回 true。
#include <iostream>
#include <stdexcept>
template <typename T>
class Vector {
public:
Vector() : capacity_(10), size_(0), data_(new T[capacity_]) {}
Vector(const Vector& other) : capacity_(other.capacity_), size_(other.size_), data_(new T[capacity_]) {
for (int i = 0; i < size_; i++) {
data_[i] = other.data_[i];
}
}
~Vector() {
delete[] data_;
}
int capacity() const {
return capacity_;
}
int size() const {
return size_;
}
bool empty() const {
return size_ == 0;
}
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;
}
}
void push_back(const T& value) {
if (size_ == capacity_) {
reserve(capacity_ * 2);
}
data_[size_] = value;
size_++;
}
void pop_back() {
if (size_ > 0) {
size_--;
}
}
T& front() {
if (size_ == 0) {
throw std::out_of_range('Vector is empty');
}
return data_[0];
}
T& back() {
if (size_ == 0) {
throw std::out_of_range('Vector is empty');
}
return data_[size_ - 1];
}
T& operator[](int i) {
return data_[i];
}
T& at(int i) {
if (i < 0 || i >= size_) {
throw std::out_of_range('Index out of range');
}
return data_[i];
}
Vector& operator=(const Vector& other) {
if (this != &other) {
delete[] data_;
capacity_ = other.capacity_;
size_ = other.size_;
data_ = new T[capacity_];
for (int i = 0; i < size_; i++) {
data_[i] = other.data_[i];
}
}
return *this;
}
void swap(Vector& other) {
std::swap(capacity_, other.capacity_);
std::swap(size_, other.size_);
std::swap(data_, other.data_);
}
bool operator==(const Vector& other) const {
if (size_ != other.size_) {
return false;
}
for (int i = 0; i < size_; i++) {
if (data_[i] != other.data_[i]) {
return false;
}
}
return true;
}
bool operator<(const Vector& other) const {
int minSize = std::min(size_, other.size_);
for (int i = 0; i < minSize; i++) {
if (data_[i] < other.data_[i]) {
return true;
} else if (data_[i] > other.data_[i]) {
return false;
}
}
return size_ < other.size_;
}
private:
int capacity_;
int size_;
T* data_;
};
int main() {
Vector<int> v1;
v1.push_back(1);
v1.push_back(2);
v1.push_back(3);
Vector<int> v2 = v1;
std::cout << 'v1 capacity: ' << v1.capacity() << std::endl;
std::cout << 'v1 size: ' << v1.size() << std::endl;
std::cout << 'v1 empty: ' << (v1.empty() ? 'true' : 'false') << std::endl;
std::cout << 'v1 front: ' << v1.front() << std::endl;
std::cout << 'v1 back: ' << v1.back() << std::endl;
std::cout << 'v1[1]: ' << v1[1] << std::endl;
std::cout << 'v2 capacity: ' << v2.capacity() << std::endl;
std::cout << 'v2 size: ' << v2.size() << std::endl;
std::cout << 'v2 empty: ' << (v2.empty() ? 'true' : 'false') << std::endl;
std::cout << 'v2 front: ' << v2.front() << std::endl;
std::cout << 'v2 back: ' << v2.back() << std::endl;
std::cout << 'v2[1]: ' << v2[1] << std::endl;
return 0;
}
本代码示例实现了一个基本的 Vector 类,包含了所需的功能。你可以根据需要进行扩展和修改。请注意,这只是一个基本的实现,可能还需要进行进一步的测试和优化。
原文地址: https://www.cveoy.top/t/topic/UaN 著作权归作者所有。请勿转载和采集!