[英]Adding objects to 2D Vector in C++
我想使用 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;
但它拋出一個錯誤:
沒有匹配的運算符 =
編輯:遵循建議並相應地更改代碼
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));
用vector<Fraction>(colunas)
linhas
副本填充向量,它本身是許多默認構造的Fraction
的colunas
向量。
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});
或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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.