I'm in the process of making a MxN Matrix Class of integers and storing the values into a 1D std::vector. When I try to add the elements using the formula for flattening a 2D vector to a 1D vector: I'm getting the vector subscript out of range error and I'm not sure what is causing it. I know that it is being thrown from my addElement()
function. Here is my class & 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;
}
EDIT
After reading a few comments: I realized that I was using std::vector<T>.reserve(count)
instead of std::vector<T>.resize(count)
After fixing that one line of code in the constructor and running the program I was also adding in the elements in main.cpp in the wrong order. The proper running code is now:
#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;
}
And this gives out the correct output:
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
Press any key & enter to quit.
Like Baum mit Augen's comment, update reserve to resize and that will fix the problem.
Reserve() function changes the vector capacity.
resize() sets the number of elements, and might set a default value if wanted.
If you think about a vector as a bucket, capacity is how big the bucket is. It's still an empty bucket until you add something. "Resizing" would set how many items you have in that bucket.
Note in the code below how the Number of elements is 0 even when the vector's capacity is increased.
#include <vector>
#include <iostream>
int main()
{
std::vector<int> MyVector;
size_t i = 0;
std::cout << "No Elements\n";
std::cout << "Capacity = " << MyVector.capacity() << "\n";
std::cout << "Number Of Elements " << MyVector.size() << "\n";
std::cout << "Resize Capacity still with no elements\n";
MyVector.reserve(10);
std::cout << "Capacity = " << MyVector.capacity() << "\n";
std::cout << "Number Of Elements " << MyVector.size() << "\n";
std::cout << "Add some Elements\n";
MyVector.push_back(++i);
MyVector.push_back(++i);
MyVector.push_back(++i);
MyVector.push_back(++i);
MyVector.push_back(++i);
std::cout << "Capacity = " << MyVector.capacity() << "\n";
std::cout << "Number Of Elements " << MyVector.size() << "\n";
return 0;
}
Results in:
No Elements
Capacity = 0
Number Of Elements 0
Resize Capacity still with no elements
Capacity = 10
Number Of Elements 0
Add some Elements
Capacity = 10
Number Of Elements 5
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments