以下是使用 C++ 实现的 Vector 类的代码,包含常用的功能和示例:

#include <iostream>
#include <algorithm>

template <typename T>
class Vector {
private:
    T* data;
    size_t capacity;
    size_t size;
public:
    // 构造函数
    Vector() : capacity(0), size(0), data(nullptr) {}
    
    // 析构函数
    ~Vector() {
        delete[] data;
    }
    
    // 复制构造函数
    Vector(const Vector& other) {
        capacity = other.capacity;
        size = other.size;
        data = new T[capacity];
        std::copy(other.data, other.data + size, data);
    }
    
    size_t capacity() const {
        return capacity;
    }
    
    size_t size() const {
        return size;
    }
    
    bool empty() const {
        return size == 0;
    }
    
    void reserve(size_t n) {
        if (n > capacity) {
            T* newData = new T[n];
            std::copy(data, data + size, newData);
            delete[] data;
            data = newData;
            capacity = n;
        }
    }
    
    void push_back(const T& value) {
        if (size == capacity) {
            reserve(capacity == 0 ? 1 : capacity * 2);
        }
        data[size] = value;
        size++;
    }
    
    void pop_back() {
        if (size > 0) {
            size--;
        }
    }
    
    T& front() const {
        return data[0];
    }
    
    T& back() const {
        return data[size - 1];
    }
    
    T& operator[](size_t i) {
        return data[i];
    }
    
    const T& at(size_t i) const {
        if (i < size) {
            return data[i];
        } else {
            throw std::out_of_range('Index out of range');
        }
    }
    
    Vector& operator=(const Vector& other) {
        if (this != &other) {
            delete[] data;
            capacity = other.capacity;
            size = other.size;
            data = new T[capacity];
            std::copy(other.data, other.data + size, data);
        }
        return *this;
    }
    
    void swap(Vector& other) {
        std::swap(data, other.data);
        std::swap(capacity, other.capacity);
        std::swap(size, other.size);
    }
    
    bool operator==(const Vector& other) const {
        if (size != other.size) {
            return false;
        }
        for (size_t i = 0; i < size; i++) {
            if (data[i] != other.data[i]) {
                return false;
            }
        }
        return true;
    }
    
    bool operator<(const Vector& other) const {
        size_t minSize = std::min(size, other.size);
        for (size_t 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;
    }
};

int main() {
    Vector<int> v;
    v.push_back(1);
    v.push_back(2);
    v.push_back(3);
    std::cout << "v.capacity(): " << v.capacity() << std::endl;
    std::cout << "v.size(): " << v.size() << std::endl;
    std::cout << "v.empty(): " << v.empty() << std::endl;
    v.reserve(10);
    std::cout << "v.capacity(): " << v.capacity() << std::endl;
    std::cout << "v.front(): " << v.front() << std::endl;
    std::cout << "v.back(): " << v.back() << std::endl;
    std::cout << "v[1]: " << v[1] << std::endl;
    std::cout << "v.at(2): " << v.at(2) << std::endl;
    Vector<int> v2 = v;
    v2.push_back(4);
    std::cout << "v == v2: " << (v == v2) << std::endl;
    std::cout << "v < v2: " << (v < v2) << std::endl;
    v.swap(v2);
    std::cout << "v.front(): " << v.front() << std::endl;
    std::cout << "v.back(): " << v.back() << std::endl;
    return 0;
}

代码说明:

  • 该代码实现了基本的 Vector 类功能,包括:
    • 构造函数、析构函数、复制构造函数
    • capacity()size()empty():获取容器容量、元素数量和判断是否为空
    • reserve():预留空间
    • push_back()pop_back():在尾部添加、删除元素
    • front()back():获取首尾元素引用
    • operator[]at():通过下标访问元素(at() 会检查越界)
    • operator=:赋值运算符
    • swap():交换两个 Vector 的内容
    • operator==operator<:比较运算符

使用示例:

代码中包含一个简单的 main 函数,演示了 Vector 类的基本用法。

注意:

  • 这只是一个简单的 Vector 类实现,可能没有考虑到所有边界情况和异常处理。
  • 在生产环境中使用前,请仔细测试和优化该代码。
  • 可以根据需要添加更多功能,例如 insert()erase() 等。

通过理解这个简单的实现,您可以更好地理解 Vector 的基本原理,并在此基础上进行扩展和应用。

其他建议:

  • 可以使用 std::vector 作为参考,了解更完善的实现。
  • 可以使用 valgrind 等工具检测内存泄漏问题。
  • 可以使用 benchmark 等工具测试性能。
C++ 实现自定义 Vector 类:功能、代码和示例

原文地址: https://www.cveoy.top/t/topic/UaY 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录