zoukankan      html  css  js  c++  java
  • 关于向量的类

    class VectorBase
    {
    int len;
    public:
    VectorBase(int len) :len(len){}
    int length()const { return len; }
    virtual double getElement(int i) const = 0;
    virtual double sum() const = 0;
    void show()const
    {
    cout << "(";
    for (int i = 0; i < length() - 1;i++)
    {
    cout << getElement(i) << ",";
    }
    cout << getElement(length()-1) << ")" << endl;
    }
    };

    class Vector :public VectorBase
    {
    double* val;
    public:
    Vector(int len, double v[] = NULL) :VectorBase(len)
    {
    val = new double[len];
    for (int i = 0; i < len;i++)
    {
    val[i] = (v == NULL ? 0.0 : v[i]);
    }
    }
    ~Vector(){ delete[] val; }
    double getElement(int index)const { return val[index]; }
    double sum()const
    {
    double s = 0.0;
    for (int i = 0; i < length();i++)
    {
    s += val[i];
    }
    return s;
    }
    };

    class ZeroVector :public VectorBase
    {
    public:
    ZeroVector(int len) :VectorBase(len){}
    double getElement(int index)const { return 0.0; }
    double sum()const { return 0.0; }
    };
    int _tmain()
    {
    VectorBase* v;
    double d[] = { 1, 2, 3, 4, 5 };
    v = new Vector(5, d);
    v->show();
    delete v;
    v = new ZeroVector(6);
    v->show();
    delete v;
    return 0;
    }

  • 相关阅读:
    7大python 深度学习框架的描述及优缺点绍
    nodejs 和 js
    python操redis
    python ConfigParser 的小技巧
    RHEL 7.3修改网卡命名规则为ethX
    redhat7.3配置163 yum源
    分区脚本(fdisk)
    linux之sed用法
    linux awk命令详解
    Python 装饰器学习
  • 原文地址:https://www.cnblogs.com/huninglei/p/5466233.html
Copyright © 2011-2022 走看看