Assigning values to 2D Vector by using indices

46,001

Solution 1

As written, this is problematic, you are trying to write to a vector for which you did not yet allocate memory.

Option 1 - Resize your vectors ahead of time

vector< vector<int> > matrix;
cout << "Filling matrix with test numbers.";
matrix.resize(4);  // resize top level vector
for (int i = 0; i < 4; i++)
{
    matrix[i].resize(4);  // resize each of the contained vectors
    for (int j = 0; j < 4; j++)
    {
        matrix[i][j] = 5;
    }
}

Option 2 - Size your vector when you declare it

vector<vector<int>>  matrix(4, vector<int>(4));

Option 3 - Use push_back to resize the vector as needed.

vector< vector<int> > matrix;
cout << "Filling matrix with test numbers.";
for (int i = 0; i < 4; i++)
{
    vector<int> temp;
    for (int j = 0; j < 4; j++)
    {
        temp.push_back(5);
    }
    matrix.push_back(temp);
}

Solution 2

You have not allocated any space for your 2d vector. So in your current code, you are trying to access some memory that does not belong to your program's memory space. This will result in Segmentation Fault.

try:

 vector<vector<int> >  matrix(4, vector<int>(4));

If you want to give all elements the same value, you can try:

 vector<vector<int> >  matrix(4, vector<int>(4,5)); // all values are now 5
Share:
46,001
AvP
Author by

AvP

Updated on July 03, 2020

Comments

  • AvP
    AvP almost 4 years

    I am trying to add values to a 2D vector by using both indices. When I run my program, I get the windows message saying the program has stopped working. Using Dev-C++ to debug showed that there was a segmentation fault (I am not sure what this means). Please do not suggest using arrays, I have to use vectors for this assignment.

    #include <iostream>
    #include <vector>
    using namespace std;
    
    int main(int argc, char** argv) { 
    
    vector< vector<int> > matrix;
    cout << "Filling matrix with test numbers.";
    for (int i = 0; i < 4; i++) {
        for (int j = 0; j < 4; j++) {
    
            matrix[i][j] = 5; // causes program to stop working
    
        }
    }
    }
    

    I have created a test case where I want to fill a 3X3 matrix with the value 5. I suspect that it has something to do with the size of the 2D vector not being specifically defined. How would I fill a 2D vector with values by using the indices?

  • AvP
    AvP almost 10 years
    Why is it necessary to have matrix[i].resize(4) inside the for loop?
  • Cory Kramer
    Cory Kramer almost 10 years
    Because you have a vector of vectors. So each of the inner vectors need a size of 4, then the outter vector contains 4 of those inner vectors.
  • AvP
    AvP almost 10 years
    For my case, I want this 2D Vector to represent a 3x3 matrix. So shouldn't it be matrix[i].resize(3), and the outer vector would contain 3 of those?
  • Cory Kramer
    Cory Kramer almost 10 years
    Yes, I just chose 4 since that's what you did in your original post. A 3x3 matrix would have valid indices of [0], [1], and [2] for the rows and columns. So you should only loop i = 0; i < 3; ++i
  • flakes
    flakes almost 10 years
    You forgot to do the top level push_back in option 3
  • flakes
    flakes almost 10 years
    @Cyber Correct me if I'm wrong (I'm a bit new at c++) but don't you have to construct the matrix[i] first before you can use it? Should it not be something more like: vector<int> temp; for(int j = 0; j < 4; j++)temp.push_back(5); matrix.push_back(temp);?
  • Shalev Shalit
    Shalev Shalit almost 9 years
    Hello Mohammad, welcome to stackoverflow. please add the main idea and changes of your answer, and not just the code.