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++ 1,132 views

## 1 answer to this question.

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.

• 4,960 points

## How to create a dynamic array of integers

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

## 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

## 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

## 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

## How to implement 2D vector array?

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

## How to implement 2D vector array?

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

## 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

## Sorting a vector of custom objects

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