小编zor*_*o47的帖子

如何将矩阵转换为一行数组

考虑一个矩阵B= [[6,4,1,2], [5,3,9,7],[1,3,2,1]];。B 是一个三行四列的矩阵。我想将它视为数组或向量,即B1=[6,4,1,2,5,3,9,7,1,3,2,1]. 此外,我想要拥有,例如B1[3]=2- 所以它是一个数字,而不是一个向量。我写了一个简单的函数

function NewArray(Matrix){
var Temp = [];
var w = Matrix[0].length;
var h = Matrix.length;
for (i=0; i<w; i++){
    for (j=0; j<h; j++){
        Temp.push(Matrix[i][j]);
        }
    }
    return Temp;
Run Code Online (Sandbox Code Playgroud)

}

然而,它只有在 B 是二次方时才有效。怎么了?

javascript

3
推荐指数
1
解决办法
1595
查看次数

通过引用在C ++中返回对象

我为自己设定的目标是重载operator+(添加类对象)。事实证明,该总和可以解释为两个向量的总和。但是当涉及到该方法时operator+,我发现很难返回该对象。我读过类似的主题,甚至尝试应用一些建议,但不幸的是没有成功。我附上一些代码。

template<class Y>
class myVect {
public:
    myVect(int n = 1);                          
    ~myVect();
    myVect(const myVect& a);                    

    myVect& operator= (const myVect&);
    myVect& operator+ (const myVect&);

    void display(const myVect& a);      

private:
    int size;
    Y* data;
    template<class U> friend class myClass;     
}; 

template<class Y>                               // constructor      
myVect<Y>::myVect(int n) {
    size = n;
    data = new Y[size];
    cout << endl << "Pass the elements" << " " << size << "\n";
    for (int i = 0; i < size; i++) { …
Run Code Online (Sandbox Code Playgroud)

c++ operator-overloading

0
推荐指数
1
解决办法
88
查看次数

标签 统计

c++ ×1

javascript ×1

operator-overloading ×1