小编Luc*_*uza的帖子

重载*,+,-'vector <double>类的运算符

我正在编写Line类来制作数值方法,并且希望这些运算符(*,+,-)使我的代码更易读和更易于理解。

        #include <vector>

        using namespace std;

        typedef vector<double> Vector;

        class Line : public Vector
        {
        public:
            Line();
            ~Line();

            Line operator+(Line);
            Line operator-(Line);
            Line operator*(double);
        };


        Line Line::operator*(double alfa)
        {
            Line temp;
            int n = size();
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i)*alfa;
            }
            return temp;
        }

        Line Line::operator+(Line line)
        {
            int n = size();
            Line temp;
            temp.resize(n);
            for (int i = 0; i < n; i++)
            {
                temp.at(i) = this->at(i) + line[i];
            } …
Run Code Online (Sandbox Code Playgroud)

c++ class operator-overloading stdvector

4
推荐指数
1
解决办法
2万
查看次数

标签 统计

c++ ×1

class ×1

operator-overloading ×1

stdvector ×1