c++ - 在C++中将对象添加到2D vector

标签 c++ matrix vector

我想使用vector制作矩阵,而不使用int,而是使用Fraction类的对象。

但是,我正在尝试向其中添加元素,但到目前为止还没有运气。

class Fraction
{
public:
    int num;
    int den;

    friend ostream& operator<< (ostream& os, Fraction& fr);
    void operator= (const Fraction& fr);
};

ostream& operator << (ostream& os, Fraction& fr)
{
     os << fr.num << "/" << fr.den;

     return os;
}

void Fraction::operator=(const Fraction& fr)
{
    num = fr.num;
    den = fr.den;
}

int main()
{
    Fraction fr;

    int colunas = 2, linhas = 2;

    vector<vector<Fraction>> mat(linhas, vector<Fraction>(colunas));

    for (int i = 0; i < colunas; ++i)
    {
        cout << endl << "Coluna " << i + 1 << endl;

        for (int j = 0; j < linhas; ++j)
        {
            int x;
            vector<Fraction> temp;
            cin >> x;

            fr.num = x;
            fr.den = 1;

            temp.push_back(fr);

            mat.push_back(temp);
        }

        cout << endl;
    }

    int len = mat.size();

    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
            {
                cout << mat[ i ] [ j ].num << " ";
            }

       cout << endl;
    }
}

这样做,我会初始化矩阵,但只会将其初始化为行。
这将是mat[0][0]mat[1][0],如果我尝试访问mat[0][1],它将给我带来垃圾。

我已经尝试过这条线
mat[i][j] = temp;

但它抛出一个错误:

no matching operator =



编辑:遵循建议并相应地更改代码
class Fraction
{
public:
    int num;
    int den;

    friend ostream& operator<< (ostream& os, Fraction& fr);
    void operator= (const Fraction& fr);
};

ostream& operator << (ostream& os, Fraction& fr)
{
    os << fr.num << "/" << fr.den;

    return os;
}

void Fraction::operator=(const Fraction& fr)
{
    num = fr.num;
    den = fr.den;
}

int main()
{
    Fraction fr;

    int colunas = 2, linhas = 2;

    vector<vector<Fraction>> mat;//(linhas, vector<Fraction>(colunas));
    vector<Fraction> temp;
    for (int i = 0; i < colunas; ++i)
    {
        cout << endl << "Coluna " << i + 1 << endl;

        for (int j = 0; j < linhas; ++j)
        {
            int x;

            cin >> x;

            fr.num = x;
            fr.den = 1;

            temp.push_back(fr);
        }

        mat.push_back(temp);

        cout << endl;
    }

    //cout << mat[0][1];
    //cin.get();
    //cin.get();

    int len = mat.size();
    for (int i = 0; i < len; i++)
    {
        for (int j = 0; j < mat[i].size(); j++)
        {
            cout << mat[ i ] [ j ].num << " ";
        }

        cout << endl;
    }
}

我希望代码执行以下操作:

当我运行它时,它的行为是这样

输入
34
3
2
4

输出
34 3
34 3 2 4

那不是我期望的输出,但是,前两个输入数字组成第一列,后两个输入数字组成第二列。所以输出应该是这样的:

预期产量
34 2
3 4

如果我访问mat[0][1],我希望它能给我2/1,但是却能给我3/1

最佳答案

此行中使用的vector构造函数:

vector<vector<Fraction>> mat(linhas, vector<Fraction>(colunas));

linhas副本的vector<Fraction>(colunas)填充 vector ,该副本本身是许多默认构造的colunasFraction的 vector 。
Faction的默认构造函数是隐式定义的(因为您自己没有声明任何构造函数),它什么也不做。它使int成员具有不确定的值。
push_back稍后会在 vector 中添加新的元素,该 vector 位于默认构造元素中已经存在的 vector 之后。 push_back不会替换 vector 中已有的元素。

您可能不想使用默认构造的元素预填充 vector ,因此只需将default-initialize mat保留为空即可。以后的push_back调用将向其中添加元素:
vector<vector<Fraction>> mat;

如评论中所述,您无需编写副本分配运算符。编译器将自动为您生成具有正确行为的编译器。

如果您想避免使用未初始化的值创建Fraction对象,例如在mat构造函数中进行的操作,则可以编写自己的构造函数(将禁用隐式声明的构造函数),例如:
class Fraction
{
public:
    int num;
    int den;

    Fraction(int num_, int den_) : num(num_), den(den_) {}

    friend ostream& operator<< (ostream& os, Fraction& fr);
    void operator= (const Fraction& fr);
};

//...

        for (int j = 0; j < linhas; ++j)
        {
            int x;
            vector<Fraction> temp;
            cin >> x;

            temp.push_back(Fraction(x, 1));

            mat.push_back(temp);
        }

除了temp.push_back(Fraction(x, 1));之外,您还可以只编写temp.push_back({x, 1});temp.emplace_back(x, 1);

您可能还打算在内部循环中收集一个内部 vector 并将其添加到外部 vector ,即:
    for (int i = 0; i < colunas; ++i)
    {
        cout << endl << "Coluna " << i + 1 << endl;

        vector<Fraction> temp;
        for (int j = 0; j < linhas; ++j)
        {
            int x;
            cin >> x;

            temp.push_back(Fraction(x,1));
        }
        mat.push_back(temp);

        cout << endl;
    }

关于c++ - 在C++中将对象添加到2D vector ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59514600/

相关文章:

c++ - EXTERN 或 STATIC 用于维护许多变量

从两个向量 (n,1) 构建 R 距离矩阵

c++ - 在 C++ 中比较 vector 的最快方法是什么?

c++ - 如果已知到平面上各个点的距离,则估计平面方程

c++ - 如何识别和比较线程 ID

python - 如何将单个 PyObject(在 C++ 后端创建)传递给嵌入式 Python 解释器?

C++14:具有通用 std::function 作为类成员的通用 lambda

python - 在 python/numpy 中优化矩阵写入

python - 基于 Numpy 中的其他数组对数组中的数据求和

Android Studio 矢量资源失真