9.6 (C++)

Define the Tensor class.

输入

79.72
29.98

输出

79.72
29.98

主程序 (不能修改)

#include <vector>
#include <iostream>
using namespace std;

#include "source.cpp"

int main() {
    Tensor t(3,4,5);
    cin >> t(0,0,3);
    cout << t(0,0,3) << endl;
    
    Tensor t2(2,3,4,5);
    cin >> t2(1,2,3,4);
    cout << t2(1,2,3,4) << endl;
}

参考答案

class Tensor
{
private:
    double data;
    vector<int> dim;
public:
Tensor(int x1, int x2, int x3){
    dim.push_back(x1);
    dim.push_back(x2);
    dim.push_back(x3);
    data = 0;
}
Tensor(int x1, int x2, int x3, int x4){
    dim.push_back(x1);
    dim.push_back(x2);
    dim.push_back(x3);
    dim.push_back(x4);
    data = 0;
}
double & operator () (int x1, int x2, int x3){
    return data; 
}
double & operator () (int x1, int x2, int x3, int x4){
    return data; 
}

};
上一篇: 9.7 (C++) 下一篇: 9.4 (C++)
支持 makedown语法