c++ - 在 C++ 中将对象添加到二维向量
问题描述
我想使用 vector 制作矩阵,而不是 int 我使用 class 的 objects 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;
但它会引发错误:
没有匹配的运算符 =
编辑:遵循建议并相应地更改代码
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)
,它本身是colunas
许多默认构造Fraction
的向量。
Faction
的默认构造函数是隐式定义的(因为您自己没有声明任何构造函数)并且它什么也不做。它使int
成员具有不确定的值。
push_back
稍后将新元素添加到向量中,在默认构造的元素中已经存在的元素之后。push_back
不会替换向量中已经存在的元素。
您可能不想用默认构造元素预填充向量,所以只需 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});
or来代替temp.emplace_back(x, 1);
。
您可能还打算在内循环中收集一个内向量并将其添加到外向量中,即:
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;
}
推荐阅读
- javascript - 相同顺序的类名产生不同的结果(Chrome、Webpack 3、CSS 模块)
- karate - 如何在 Karate.write 函数中使用相对或绝对文件路径
- php - php Mysql单独显示joined query
- python - 在python中以任意音高合成声音
- javascript - 仅使用 javascript 和 html 读取 csv 或 excel (xlsx) 文件?
- groovy - 如何在groovy中将输入转换为大写
- javascript - 检查列标题标题是否与字符串匹配,如果匹配则返回列索引
- android - 可以为库项目跳过 TargetSdkVersion(27 到 29)吗?
- javascript - 如何为这段代码编写 Jasmine 单元测试
- angular -
从上层组件。角 7