C++ Vector Subscript of of range

Francis Cugler

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.
Tono

Like Baum mit Augen's comment, update reserve to resize and that will fix the problem.

Reserve

Reserve() function changes the vector capacity.

Resize

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.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

C++ Vector Range Constructor

From Dev

VBA - Subscript out of range

From Dev

Debug assertion failed. C++ vector subscript out of range

From Dev

Why is my program having an instance where "Vector Subscript out of Range"?

From Dev

C++ vector subscript out of range

From Dev

C++ custom vector implementation – subscript of pointer to incomplete type

From Dev

Why am I getting 'vector subscript out of range'?

From Dev

Vector Subscript Out of Range While Trying to Populate a Struct with a Vector

From Dev

C++ Vector subscript out of range dirent

From Dev

C++ 2d Vector subscript out of range error

From Dev

Vector subscript out of range - C++

From Dev

Debug assertion failed.. C++ vector subscript out of range

From Dev

Vector subscript out of range on iterator

From Dev

C++ string library error: string subscript out of range

From Dev

VBA - Subscript out of range

From Dev

Debug assertion failed. C++ vector subscript out of range

From Dev

Subscript out of range ,VBA

From Dev

vector subscript out of range error message3

From Dev

String Subscript out of range in C++

From Dev

VBA: Subscript out of range

From Dev

OpenCV / Visual C++ : Vector Subscript Out of Range

From Dev

c++ Vector subscript out of range error

From Dev

C++ Vector subscript out of range line 1201

From Dev

C++ Debug assertion failed. vector subscript out of range

From Dev

Vector subscript out of range, PCL octree

From Dev

Why when I'm trying to insert into a vector of sets it's telling me "vector subscript out of range"?

From Dev

find() returns vector subscript out of range

From Dev

SDL vector subscript out of range

From Dev

Why am I getting vector subscript out of range?

Related Related

HotTag

Archive