问题描述
问题:我使用相同的解决方案重载了运算符*
和*=
,尽管使用运算符*=
似乎并没有改变Matrix的内容,也许我在错误地声明了运算符重载方法.
与此同时,运算符*
可以正常工作,并且实际上可以乘以Matrix,我已经事先对其进行了检查.
输出:
3 4 -5
8 0 7
8 9 -4
8 7 7
-6 0 6
2 2 9
3 4 -5
8 0 7
8 9 -4
这是代码本身:
struct WrappedMatrix{
int n;
int ** Matrix;
};
struct WrappedVector{
int n;
int * Vector;
};
WrappedVector linearizedMatrix(WrappedMatrix matrix){
WrappedVector vector;
vector.n = matrix.n * matrix.n;
vector.Vector = new int[vector.n];
for(int i = 0; i < matrix.n; i++){
for(int j = 0; j < matrix.n; j++){
int k = j + (int) (i*sqrt(vector.n));
vector.Vector[k] = matrix.Matrix[i][j];
}
}
return vector;
}
WrappedMatrix normalMatrix(WrappedVector vector){
WrappedMatrix matrix;
matrix.n = sqrt(vector.n);
matrix.Matrix = new int * [matrix.n];
for(int i = 0; i < matrix.n; i++){
matrix.Matrix[i] = new int[matrix.n];
for(int j = 0; j < matrix.n; j++){
int k = j + (int) (i*sqrt(vector.n));
matrix.Matrix[i][j] = vector.Vector[k];
}
}
return matrix;
}
WrappedVector operator*(const WrappedVector& vector1, const WrappedVector& vector2) {
if(vector1.n != vector2.n) {
cout << "Матриці різних розмірів!" << endl;
return vector1;
}
WrappedMatrix matrix1 = normalMatrix(vector1);
WrappedMatrix matrix2 = normalMatrix(vector2);
WrappedMatrix result;
result.n = matrix1.n;
result.Matrix = new int * [result.n];
for(int i = 0; i < result.n; i++){
result.Matrix[i] = new int[result.n];
}
for(int i = 0; i < result.n; i++){
for(int j = 0; j < result.n; j++){
for(int k = 0; k < result.n; k++){
int p1 = matrix1.Matrix[i][k];
int p2 = matrix2.Matrix[k][j];
result.Matrix[i][j] += p1 * p2;
}
}
}
WrappedVector resultV = linearizedMatrix(result);
return resultV;
}
//?
WrappedVector operator*=(const WrappedVector& vector1, const WrappedVector& vector2) {
if(vector1.n != vector2.n) {
cout << "Матриці різних розмірів!" << endl;
return vector1;
}
WrappedMatrix matrix1 = normalMatrix(vector1);
WrappedMatrix matrix2 = normalMatrix(vector2);
WrappedMatrix result;
result.n = matrix1.n;
result.Matrix = new int * [result.n];
for(int i = 0; i < result.n; i++){
result.Matrix[i] = new int[result.n];
}
for(int i = 0; i < result.n; i++){
for(int j = 0; j < result.n; j++){
for(int k = 0; k < result.n; k++){
int p1 = matrix1.Matrix[i][k];
int p2 = matrix2.Matrix[k][j];
result.Matrix[i][j] += p1 * p2;
}
}
}
WrappedVector resultV = linearizedMatrix(result);
return resultV;
}
int main() {
WrappedMatrix matrix;
matrix.n = 3;
matrix.Matrix = new int * [matrix.n];
matrix.Matrix[0] = new int[matrix.n];
matrix.Matrix[1] = new int[matrix.n];
matrix.Matrix[2] = new int[matrix.n];
matrix.Matrix[0][0] = 3;
matrix.Matrix[0][1] = 4;
matrix.Matrix[0][2] = -5;
matrix.Matrix[1][0] = 8;
matrix.Matrix[1][1] = 0;
matrix.Matrix[1][2] = 7;
matrix.Matrix[2][0] = 8;
matrix.Matrix[2][1] = 9;
matrix.Matrix[2][2] = -4;
WrappedVector vector = linearizedMatrix(matrix);
cout << vector << endl;
WrappedMatrix matrix1;
matrix1.n = 3;
matrix1.Matrix = new int * [matrix1.n];
matrix1.Matrix[0] = new int[matrix1.n];
matrix1.Matrix[1] = new int[matrix1.n];
matrix1.Matrix[2] = new int[matrix1.n];
matrix1.Matrix[0][0] = 8;
matrix1.Matrix[0][1] = 7;
matrix1.Matrix[0][2] = 7;
matrix1.Matrix[1][0] = -6;
matrix1.Matrix[1][1] = 0;
matrix1.Matrix[1][2] = 6;
matrix1.Matrix[2][0] = 2;
matrix1.Matrix[2][1] = 2;
matrix1.Matrix[2][2] = 9;
WrappedVector vector1 = linearizedMatrix(matrix1);
cout << vector1 << endl;
vector *= vector1;
cout << vector;
return 0;
}
提前谢谢!
从技术上讲,这不是一个答案,到目前为止,我只是修改了代码,如果有时间,我会在今晚晚些时候添加到代码上.今天早上我刚刚设法整理好了一些东西,以为您不妨看看一下,这样它就不会只是无所事事了:
#include <iostream>
#include <vector>
#include <cstdarg>
class matrix{
public:
matrix(){};
matrix(std::initializer_list<std::vector<int>> vectors):x(vectors){}
~matrix(){};
const int& size(){ return this->x.size(); } //# of vectors
//ALT: ex. print: 3x3, 4x5, 7x3
//void size(){ std::cout<<"Dim: "<<this->x.size<<"x"<<this->x.front().size()<<std::endl;
void add(const std::vector<int>& arr){ this->x.push_back(arr); }
const std::vector<std::vector<int>>& extract(){ return this->x; } //Returns entire matrix
const std::vector<int>& getVector(const int& row){ return this->x.at(row); } //Returns specific vector from matrix
matrix operator*(const matrix& m){ //More params
//Stuff here
}
matrix operator*=(const matrix& m){ //More params
//Stuff here
}
private:
std::vector<std::vector<int>> x;
};
//Can break the center out to make one for vectors too
std::ostream& operator<<(std::ostream& os, matrix& m){
for (auto& it:m.extract()){
for (auto& jt:it){
os<<jt<<" ";
}
os<<std::endl;
}
os<<std::endl;
return os;
}
int main(){
matrix m({{2,5,8,11,14},
{3,6,9,12,15},
{4,7,10,13,16}});
std::cout<<m;
return 0;
}
根据我的收集,这是关于评论的内容.在我看来,使用起来要容易得多,但是如果需要以某种特定方式构造结构和访问矢量,它可能会比您需要的东西更简单.
此外,我没有弄清楚您在脑子里想得到什么数学结果,因此我没有在运算符中添加任何内容,如果您正在做的话,我稍后可能会添加一些叉积或点积示例. /p>
Problem: I have overloaded operators *
and *=
with the same solution, though using operator *=
doesn't seem to change the contents of the Matrix, maybe I am declaring the operator overload method incorrectly.
At the same time, operator *
works properly and actually multiplies Matrix, I have checked it beforehand.
Output:
3 4 -5
8 0 7
8 9 -4
8 7 7
-6 0 6
2 2 9
3 4 -5
8 0 7
8 9 -4
Here is the code itself:
struct WrappedMatrix{
int n;
int ** Matrix;
};
struct WrappedVector{
int n;
int * Vector;
};
WrappedVector linearizedMatrix(WrappedMatrix matrix){
WrappedVector vector;
vector.n = matrix.n * matrix.n;
vector.Vector = new int[vector.n];
for(int i = 0; i < matrix.n; i++){
for(int j = 0; j < matrix.n; j++){
int k = j + (int) (i*sqrt(vector.n));
vector.Vector[k] = matrix.Matrix[i][j];
}
}
return vector;
}
WrappedMatrix normalMatrix(WrappedVector vector){
WrappedMatrix matrix;
matrix.n = sqrt(vector.n);
matrix.Matrix = new int * [matrix.n];
for(int i = 0; i < matrix.n; i++){
matrix.Matrix[i] = new int[matrix.n];
for(int j = 0; j < matrix.n; j++){
int k = j + (int) (i*sqrt(vector.n));
matrix.Matrix[i][j] = vector.Vector[k];
}
}
return matrix;
}
WrappedVector operator*(const WrappedVector& vector1, const WrappedVector& vector2) {
if(vector1.n != vector2.n) {
cout << "Матриці різних розмірів!" << endl;
return vector1;
}
WrappedMatrix matrix1 = normalMatrix(vector1);
WrappedMatrix matrix2 = normalMatrix(vector2);
WrappedMatrix result;
result.n = matrix1.n;
result.Matrix = new int * [result.n];
for(int i = 0; i < result.n; i++){
result.Matrix[i] = new int[result.n];
}
for(int i = 0; i < result.n; i++){
for(int j = 0; j < result.n; j++){
for(int k = 0; k < result.n; k++){
int p1 = matrix1.Matrix[i][k];
int p2 = matrix2.Matrix[k][j];
result.Matrix[i][j] += p1 * p2;
}
}
}
WrappedVector resultV = linearizedMatrix(result);
return resultV;
}
//?
WrappedVector operator*=(const WrappedVector& vector1, const WrappedVector& vector2) {
if(vector1.n != vector2.n) {
cout << "Матриці різних розмірів!" << endl;
return vector1;
}
WrappedMatrix matrix1 = normalMatrix(vector1);
WrappedMatrix matrix2 = normalMatrix(vector2);
WrappedMatrix result;
result.n = matrix1.n;
result.Matrix = new int * [result.n];
for(int i = 0; i < result.n; i++){
result.Matrix[i] = new int[result.n];
}
for(int i = 0; i < result.n; i++){
for(int j = 0; j < result.n; j++){
for(int k = 0; k < result.n; k++){
int p1 = matrix1.Matrix[i][k];
int p2 = matrix2.Matrix[k][j];
result.Matrix[i][j] += p1 * p2;
}
}
}
WrappedVector resultV = linearizedMatrix(result);
return resultV;
}
int main() {
WrappedMatrix matrix;
matrix.n = 3;
matrix.Matrix = new int * [matrix.n];
matrix.Matrix[0] = new int[matrix.n];
matrix.Matrix[1] = new int[matrix.n];
matrix.Matrix[2] = new int[matrix.n];
matrix.Matrix[0][0] = 3;
matrix.Matrix[0][1] = 4;
matrix.Matrix[0][2] = -5;
matrix.Matrix[1][0] = 8;
matrix.Matrix[1][1] = 0;
matrix.Matrix[1][2] = 7;
matrix.Matrix[2][0] = 8;
matrix.Matrix[2][1] = 9;
matrix.Matrix[2][2] = -4;
WrappedVector vector = linearizedMatrix(matrix);
cout << vector << endl;
WrappedMatrix matrix1;
matrix1.n = 3;
matrix1.Matrix = new int * [matrix1.n];
matrix1.Matrix[0] = new int[matrix1.n];
matrix1.Matrix[1] = new int[matrix1.n];
matrix1.Matrix[2] = new int[matrix1.n];
matrix1.Matrix[0][0] = 8;
matrix1.Matrix[0][1] = 7;
matrix1.Matrix[0][2] = 7;
matrix1.Matrix[1][0] = -6;
matrix1.Matrix[1][1] = 0;
matrix1.Matrix[1][2] = 6;
matrix1.Matrix[2][0] = 2;
matrix1.Matrix[2][1] = 2;
matrix1.Matrix[2][2] = 9;
WrappedVector vector1 = linearizedMatrix(matrix1);
cout << vector1 << endl;
vector *= vector1;
cout << vector;
return 0;
}
Thank you in advance!
This isn't an answer technically, I just revamped the code thus far, I'll add onto it later tonight if I have time. I just managed to put something together this morning, figured you might as well have a look so it doesn't just lay around doing nothing:
#include <iostream>
#include <vector>
#include <cstdarg>
class matrix{
public:
matrix(){};
matrix(std::initializer_list<std::vector<int>> vectors):x(vectors){}
~matrix(){};
const int& size(){ return this->x.size(); } //# of vectors
//ALT: ex. print: 3x3, 4x5, 7x3
//void size(){ std::cout<<"Dim: "<<this->x.size<<"x"<<this->x.front().size()<<std::endl;
void add(const std::vector<int>& arr){ this->x.push_back(arr); }
const std::vector<std::vector<int>>& extract(){ return this->x; } //Returns entire matrix
const std::vector<int>& getVector(const int& row){ return this->x.at(row); } //Returns specific vector from matrix
matrix operator*(const matrix& m){ //More params
//Stuff here
}
matrix operator*=(const matrix& m){ //More params
//Stuff here
}
private:
std::vector<std::vector<int>> x;
};
//Can break the center out to make one for vectors too
std::ostream& operator<<(std::ostream& os, matrix& m){
for (auto& it:m.extract()){
for (auto& jt:it){
os<<jt<<" ";
}
os<<std::endl;
}
os<<std::endl;
return os;
}
int main(){
matrix m({{2,5,8,11,14},
{3,6,9,12,15},
{4,7,10,13,16}});
std::cout<<m;
return 0;
}
This is, from what I gather, what the comments were about. Much easier to use in my opinion, though it may be something other than what you need if you are demanded to make structs and access vectors in some particular way.
Also, I didn't figure out what mathematical result you had in mind there, so I didn't put anything into the operators, I might add some crossproduct or dotproduct example later on, if that's what you were doing.
这篇关于尝试在C ++中重新加载运算符,但似乎不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!