【发布时间】:2017-11-19 19:00:55
【问题描述】:
我正在制作整数的 MxN 矩阵类并将值存储到一维 std::vector 中。当我尝试使用将 2D 向量展平为 1D 向量的公式添加元素时:我得到向量下标超出范围错误,我不确定是什么原因造成的。我知道它是从我的addElement() 函数中抛出的。这是我的课程和 main.cpp
#include <vector>
#include <iostream>
template<unsigned Col, unsigned Row>
class Matrix2D {
public:
const unsigned col_size = Col;
const unsigned row_size = Row;
const unsigned stride_ = col_size;
const unsigned matrix_size = col_size * row_size;
private:
std::vector<int> data_;
public:
Matrix2D() {
data_.reserve( col_size * row_size );
}
void addElement( unsigned x, unsigned y, int val ) {
data_[(x * row_size + y)]= val;
}
int getElement( unsigned x, unsigned y ) {
return data_[(x * row_size + y)];
}
int getElement( unsigned idx ) {
return data_[idx];
}
};
int main() {
Matrix2D<4, 4> mat4x4;
mat4x4.addElement( 0, 0, 0 );
mat4x4.addElement( 1, 0, 1 );
mat4x4.addElement( 2, 0, 2 );
mat4x4.addElement( 3, 0, 3 );
mat4x4.addElement( 0, 1, 4 );
mat4x4.addElement( 1, 1, 5 );
mat4x4.addElement( 2, 1, 6 );
mat4x4.addElement( 3, 1, 7 );
mat4x4.addElement( 0, 2, 8 );
mat4x4.addElement( 1, 2, 9 );
mat4x4.addElement( 2, 2, 10 );
mat4x4.addElement( 3, 2, 11 );
mat4x4.addElement( 0, 3, 12 );
mat4x4.addElement( 1, 3, 13 );
mat4x4.addElement( 2, 3, 14 );
mat4x4.addElement( 3, 3, 15 );
for ( unsigned i = 0; i < mat4x4.matrix_size; i++ ) {
std::cout << mat4x4.getElement( i ) << "\n";
}
std::cout << "\nPress any key & enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
编辑
读了几篇cmets后:我意识到我用的是std::vector<T>.reserve(count)而不是std::vector<T>.resize(count)
在构造函数中修复了那一行代码并运行程序后,我还在 main.cpp 中以错误的顺序添加了元素。现在正确的运行代码是:
#include <iostream>
#include <vector>
template<unsigned Col, unsigned Row>
class Matrix2D {
public:
const unsigned col_size = Col;
const unsigned row_size = Row;
const unsigned stride_ = col_size;
const unsigned matrix_size = col_size * row_size;
private:
std::vector<int> data_;
public:
Matrix2D() {
data_.resize( col_size * row_size );
}
void addElement( unsigned x, unsigned y, int val ) {
data_[(x * row_size + y)]= val;
}
int getElement( unsigned x, unsigned y ) {
return data_[(x * row_size + y)];
}
int getElement( unsigned idx ) {
return data_[idx];
}
};
int main() {
Matrix2D<4, 4> mat4x4;
mat4x4.addElement( 0, 0, 0 );
mat4x4.addElement( 0, 1, 1 );
mat4x4.addElement( 0, 2, 2 );
mat4x4.addElement( 0, 3, 3 );
mat4x4.addElement( 1, 0, 4 );
mat4x4.addElement( 1, 1, 5 );
mat4x4.addElement( 1, 2, 6 );
mat4x4.addElement( 1, 3, 7 );
mat4x4.addElement( 2, 0, 8 );
mat4x4.addElement( 2, 1, 9 );
mat4x4.addElement( 2, 2, 10 );
mat4x4.addElement( 2, 3, 11 );
mat4x4.addElement( 3, 0, 12 );
mat4x4.addElement( 3, 1, 13 );
mat4x4.addElement( 3, 2, 14 );
mat4x4.addElement( 3, 3, 15 );
for ( unsigned i = 0; i < mat4x4.matrix_size; i++ ) {
std::cout << mat4x4.getElement( i ) << "\n";
}
std::cout << "\nPress any key & enter to quit." << std::endl;
char c;
std::cin >> c;
return 0;
}
这给出了正确的输出:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Press any key & enter to quit.
【问题讨论】:
-
.resize,而不是reserve。投票结束是错字,如果不是,请阅读文档。 -
Matrix2D() : data_( col_size * row_size ){} -
哇;我不敢相信我忽略了我使用
vector<T>.reserve()而不是vector<T>.resize()的事实。感谢您的帮助;一个多小时我都无法绕开它……