Vector of Vectors to create matrix

0 votes

I'm attempting to get the dimensions of a 2D matrix as an input. Then utilize user input to fill in the blanks in this matrix. I attempted to accomplish this using vectors (vectors of vectors). However, anytime I try to read in data and append it to the matrix, I get problems.

//cin>>CC; cin>>RR; already done
vector<vector<int> > matrix;
for(int i = 0; i<RR; i++)
{
    for(int j = 0; j<CC; j++)
    {
    cout<<"Enter the number for Matrix 1";
         cin>>matrix[i][j];
    }
}

I get a subscript out of range error whenever I try to execute this. 

Do you have any suggestions?

Jun 15, 2022 in C++ by Nicholas
• 7,760 points
1,406 views

1 answer to this question.

0 votes

Before accessing any elements, you must first set the vector of vectors to the right size. 

You may do it this way:

// assumes using std::vector for brevity
vector<vector<int>> matrix(RR, vector<int>(CC));

This generates a vector filled with RR size CC vectors.

answered Jun 16, 2022 by Damon
• 4,960 points

Related Questions In C++

0 votes
0 answers

How to create a dynamic array of integers

How can I use the new keyword ...READ MORE

Jul 15, 2022 in C++ by Nicholas
• 7,760 points
396 views
0 votes
0 answers

Adding to a vector of pair

I have a pair vector that looks like this: vector<pair<string,double>> revenue; I'd want to add a string and a double from a map that looks like this: revenue[i].first = "string"; revenue[i].second = map[i].second; However, because revenue is not initialised, it returns an out of bounds error.  So I ...READ MORE

Jul 26, 2022 in C++ by Nicholas
• 7,760 points
724 views
0 votes
0 answers

Correct way to work with vector of arrays

Could someone please explain how to work ...READ MORE

Aug 23, 2022 in C++ by Nicholas
• 7,760 points
593 views
0 votes
1 answer

Lower and Upper Bound in case of Decreasing/Non-ascending vector

Both std::lower bound and std::upper bound must have an increasing (non-decreasing) order as their objective. By giving a comparator as the 4th parameter of the functions, you may modify the meaning of "growing." To work with descending vectors, use std::greater. #include<iostream> #include<vector> #include<algorithm> #include<functional> using namespace std; int main() { ...READ MORE

answered Jun 14, 2022 in C++ by Damon
• 4,960 points
1,212 views
0 votes
1 answer

How to find out if an item is present in an std::vector?

The most straightforward solution is to count the total number of elements in the vector that have the specified value.  If the count is greater than zero, we've found our element.  This is simple to accomplish with the std::count function. #include <iostream> #include <vector> #include <algorithm> int main() { ...READ MORE

answered May 27, 2022 in Others by Damon
• 4,960 points
14,114 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
531 views
0 votes
0 answers

How to implement 2D vector array?

I'm using the vector class in the ...READ MORE

Jun 1, 2022 in C++ by Nicholas
• 7,760 points
395 views
0 votes
1 answer

How do I erase an element from std::vector<> by index?

You might perform the following to remove a single element: std::vector<int> vec; vec.push_back(6); vec.push_back(-17); vec.push_back(12); // Deletes the second element (vec[1]) vec.erase(std::next(vec.begin())); Alternatively, to remove many elements at once: // ...READ MORE

answered Jun 10, 2022 in C++ by Damon
• 4,960 points
1,481 views
0 votes
1 answer

How do i apply lower_bound to a range of unsorted vector elements?

What's the point of sorting your array? ...READ MORE

answered Jun 15, 2022 in C++ by Damon
• 4,960 points
1,763 views
0 votes
1 answer

Sorting a vector of custom objects

A simple example using std::sort struct MyStruct { ...READ MORE

answered Jun 1, 2022 in C++ by Damon
• 4,960 points
1,264 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP