【发布时间】:2018-10-04 14:57:13
【问题描述】:
我对使用向量和一般的 C++ 编码还很陌生,但还没有完全掌握这门语言。我的查询如下:
1. 我的主要问题似乎是我的 transform 行,为什么会这样?
2. 如何打印 A 和 B 的向量和?
3.如何重载[][]操作符进行访问并使其工作? (即,如果编写了 Mat[1][3] = 4,代码应该仍然有效)
#include <iostream>
#include <algorithm>
#include <vector>
#include <functional>
using namespace std;
class Matrix
{
public:
double x;
vector<vector<double> > I{ { 1, 0, 0, 0 },
{ 0, 1, 0, 0 },
{ 0, 0, 1, 0 },
{ 0, 0, 0, 1 } };
vector<vector<double> > Initialization(vector<vector<double> > I, double x);
vector<vector<double> > Copy(vector<vector<double> > I);
void Print(vector<vector<double> > I);
};
vector<vector<double> > Matrix::Initialization(vector<vector<double> > I, double x)
{
for (int i = 0; i < I.size(); i++) {
for (int j = 0; j < I[i].size(); j++)
{
// new matrix
I[i][j] *= x;
}
}
return I;
};
vector<vector<double> > Matrix::Copy(vector<vector<double> > I)
{
vector<vector<double> > I_copy = I;
return I_copy;
};
void Matrix::Print(vector<vector<double> > I)
{
for (int i = 0; i < I.size(); i++) {
for (int j = 0; j < I[i].size(); j++)
{
cout << I[i][j] << " ";
}
cout << endl;
}
};
int main()
{
Matrix m;
vector<vector<double> > A;
vector<vector<double> > B;
cin >> m.x;
A = m.Initialization(m.I, m.x);
B = m.Copy(A);
m.Print(A);
m.Print(B);
B.resize(A.size());
transform(A.begin(), A.end(), B.begin(), A.begin(), plus<double>());
return 0;
}
我希望您能耐心地帮助我修复我的代码,并让我理解为什么我的语法不正确且无法编译。非常感谢
【问题讨论】:
-
一个问题一个问题。
-
您在转换后尝试
m.Print(A)吗?矩阵转换了吗? -
你需要
plus<vector<double>>()这是无效的。 -
"我的主要问题似乎是我的变换线,为什么会这样?"我不知道这个问题是什么意思。它读起来像“为什么是地球?”;它的形成就像一个问题,但不是一个明智的问题。
-
这些是正确的迭代器吗? 变换(A.begin(), A.end(), B.begin(), B.end(), 加
());
标签: c++ vector sum overloading addition