Assigning values to 2D Vector by using indices
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
![AvP](https://lh4.googleusercontent.com/-1pexhy8O0uE/AAAAAAAAAAI/AAAAAAAAAGM/nw-YK7OcLiI/photo.jpg?sz=256)
AvP
Updated on July 03, 2020Comments
-
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 almost 10 yearsWhy is it necessary to have matrix[i].resize(4) inside the for loop?
-
Cory Kramer almost 10 yearsBecause 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 almost 10 yearsFor 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 almost 10 yearsYes, 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 loopi = 0; i < 3; ++i
-
flakes almost 10 yearsYou forgot to do the top level
push_back
in option 3 -
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 almost 9 yearsHello Mohammad, welcome to stackoverflow. please add the main idea and changes of your answer, and not just the code.