0
我正在写的程序的一部分要求我设置矢量矢量,以便成为维度为5的方形矩阵。尝试打印时似乎没有输出出矢量,我不知道为什么。有小费吗?使用矢量向量设置单位矩阵C++
#include<string>
#include<cstdlib>
#include<fstream>
#include<vector>
#include<iostream>
using namespace std;
int main(){
int rows=5;
vector< vector<double> > identity; // declare another vector of vectors, which initially will be
// the identity matrix of the same dimensions as 'matrix'.
for (int j=0; j<rows; j++) {//set up the identity matrix using the Kronecker Delta relation. If row == col, then =1. Else =0.
vector<double> temp2; // temporary vector to push onto identity
identity.push_back(temp2);
for (int k=0; k<rows; k++){
if(j==k) {
temp2.push_back(1);
}
else {
temp2.push_back(0);
}
}
identity.push_back(temp2);
}
// print out identity
for (int j=0; j<identity.size(); j++) {
for (int k=0; k<identity[0].size(); k++) {
cout<<' '<<identity[j][k];
}
cout<<endl;
}
}
你为什么将'temp2'推到'identity'上两次? –
哎呀,我现在检查一下。编辑:这工作,我知道我一定是一直在某个地方愚蠢。干杯:) – user3000403
这是你的错误。 '身份[0] .size()'为0. –