C++ 向量类模板实现:用于高效的向量操作

本代码展示了使用 C++ 模板实现的向量类,该类允许您创建任意类型的向量对象,并提供向量加法、模长计算、元素访问等操作。

#include <iostream>
#include <cmath>
#include <stdexcept>

template <typename T>
class Vector {
public:
    // 默认构造函数
    Vector() : data(nullptr), size(0) {}

    // 带参构造函数
    Vector(int dim) : size(dim) {
        data = new T[size];
    }

    // 拷贝构造函数
    Vector(const Vector& other) : size(other.size) {
        data = new T[size];
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }

    // 析构函数
    ~Vector() {
        delete[] data;
    }

    // 重载 [] 运算符以允许访问向量元素
    T& operator[](int index) {
        if (index >= 0 && index < size) {
            return data[index];
        } else {
            throw std::out_of_range("Index out of range");
        }
    }

    // 重载 const 版本的 [] 运算符,以允许在常量对象上访问元素
    const T& operator[](int index) const {
        if (index >= 0 && index < size) {
            return data[index];
        } else {
            throw std::out_of_range("Index out of range");
        }
    }

    // 深赋值操作符
    Vector& operator=(const Vector& other) {
        if (this == &other) {
            return *this;
        }

        delete[] data;
        size = other.size;
        data = new T[size];
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }

        return *this;
    }

    // 获取向量模长
    double getMagnitude() const {
        double sum = 0.0;
        for (int i = 0; i < size; i++) {
            sum += data[i] * data[i];
        }
        return std::sqrt(sum);
    }

    // 向量的输出
    void VShow() {
        std::cout << "The vector is : ( ";
        for (int i = 0; i < size - 1; i++) {
            std::cout << data[i] << " , ";
        }
        std::cout << data[size - 1] << " )" << std::endl;
    }

    // 向量加法
    Vector operator+(const Vector& other) const {
        if (size != other.size) {
            throw std::invalid_argument("Vector sizes are not equal");
        }

        Vector result(size);
        for (int i = 0; i < size; i++) {
            result.data[i] = data[i] + other.data[i];
        }

        return result;
    }

private:
    T* data;
    int size;
};

现在,您可以使用这个向量类模板来创建任意类型的向量对象,并进行向量加法等操作。例如:

int main() {
    // 创建两个向量对象
    Vector<int> v1(3);
    Vector<int> v2(3);

    // 设置向量元素的值
    v1[0] = 1;
    v1[1] = 2;
    v1[2] = 3;

    v2[0] = 4;
    v2[1] = 5;
    v2[2] = 6;

    // 输出向量内容
    v1.VShow();
    v2.VShow();

    // 计算向量的模长
    double magnitude = v1.getMagnitude();
    std::cout << "Magnitude of v1: " << magnitude << std::endl;

    // 进行向量加法
    Vector<int> sum = v1 + v2;
    sum.VShow();

    return 0;
}

这样,您就可以根据需要使用这个向量类模板进行向量操作了。

使用说明:

  1. 创建向量对象: 使用 Vector<T>(dim) 创建一个维度为 dim 的向量对象,T 是元素类型。
  2. 访问元素: 使用 [] 运算符访问向量元素,例如 v1[0] 访问第一个元素。
  3. 向量加法: 使用 + 运算符进行向量加法,例如 sum = v1 + v2
  4. 计算模长: 使用 getMagnitude() 方法计算向量模长。
  5. 输出向量: 使用 VShow() 方法输出向量内容。

注意:

  • 所有操作都进行了边界检查,以防止越界访问。
  • 向量加法要求两个向量的维度相同。

更多功能:

您可以根据需要扩展此向量类模板,添加更多功能,例如:

  • 向量减法
  • 向量乘法
  • 向量点积
  • 向量叉积
  • 归一化向量
  • 向量旋转

示例代码:

#include <iostream>
#include <cmath>
#include <stdexcept>

template <typename T>
class Vector {
public:
    // 默认构造函数
    Vector() : data(nullptr), size(0) {}

    // 带参构造函数
    Vector(int dim) : size(dim) {
        data = new T[size];
    }

    // 拷贝构造函数
    Vector(const Vector& other) : size(other.size) {
        data = new T[size];
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }
    }

    // 析构函数
    ~Vector() {
        delete[] data;
    }

    // 重载 [] 运算符以允许访问向量元素
    T& operator[](int index) {
        if (index >= 0 && index < size) {
            return data[index];
        } else {
            throw std::out_of_range("Index out of range");
        }
    }

    // 重载 const 版本的 [] 运算符,以允许在常量对象上访问元素
    const T& operator[](int index) const {
        if (index >= 0 && index < size) {
            return data[index];
        } else {
            throw std::out_of_range("Index out of range");
        }
    }

    // 深赋值操作符
    Vector& operator=(const Vector& other) {
        if (this == &other) {
            return *this;
        }

        delete[] data;
        size = other.size;
        data = new T[size];
        for (int i = 0; i < size; i++) {
            data[i] = other.data[i];
        }

        return *this;
    }

    // 获取向量模长
    double getMagnitude() const {
        double sum = 0.0;
        for (int i = 0; i < size; i++) {
            sum += data[i] * data[i];
        }
        return std::sqrt(sum);
    }

    // 向量的输出
    void VShow() {
        std::cout << "The vector is : ( ";
        for (int i = 0; i < size - 1; i++) {
            std::cout << data[i] << " , ";
        }
        std::cout << data[size - 1] << " )" << std::endl;
    }

    // 向量加法
    Vector operator+(const Vector& other) const {
        if (size != other.size) {
            throw std::invalid_argument("Vector sizes are not equal");
        }

        Vector result(size);
        for (int i = 0; i < size; i++) {
            result.data[i] = data[i] + other.data[i];
        }

        return result;
    }

private:
    T* data;
    int size;
};

int main() {
    // 创建两个向量对象
    Vector<int> v1(3);
    Vector<int> v2(3);

    // 设置向量元素的值
    v1[0] = 1;
    v1[1] = 2;
    v1[2] = 3;

    v2[0] = 4;
    v2[1] = 5;
    v2[2] = 6;

    // 输出向量内容
    v1.VShow();
    v2.VShow();

    // 计算向量的模长
    double magnitude = v1.getMagnitude();
    std::cout << "Magnitude of v1: " << magnitude << std::endl;

    // 进行向量加法
    Vector<int> sum = v1 + v2;
    sum.VShow();

    return 0;
}
C++ 向量类模板实现:用于高效的向量操作

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

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