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 in C++ by Nicholas
• 2,460 points
5 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 by Damon
• 3,580 points

Related Questions In C++

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 in C++ by Damon
• 3,580 points
6 views
0 votes
0 answers

What is the easiest way to initialize a std::vector with hardcoded elements?

I can make an array and initialise&nb ...READ MORE

1 day ago in C++ by Nicholas
• 2,460 points
6 views
0 votes
1 answer

Syntax of priority queue

We must first include the queue header file in order to establish a priority queue in C++. #include <queue> Once we import this file, we ...READ MORE

answered May 31 in C++ by Damon
• 3,580 points
11 views
0 votes
1 answer

Easiest way to convert int to string in C++

C++ adds std::stoi (and variants for each numeric type) and std::to string, which are the C equivalents of atoi and itoa but expressed in terms of std::string #include <string> std::string s = std::to_string(42); Is therefore ...READ MORE

answered Jun 1 in C++ by Damon
• 3,580 points
25 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 in Others by Damon
• 3,580 points
12 views
0 votes
0 answers

How to implement 2D vector array?

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

Jun 1 in C++ by Nicholas
• 2,460 points
11 views
0 votes
0 answers

How to implement 2D vector array?

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

Jun 1 in C++ by Nicholas
• 2,460 points
7 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 in C++ by Damon
• 3,580 points
14 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 in C++ by Damon
• 3,580 points
15 views
0 votes
1 answer

Sorting a vector of custom objects

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

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