Answers for "vector 2d arrays"

C++
2

2d vector

#include <bits/stdc++.h>
using namespace std;
int main() 
{
  int rows = 2;
  int cols = 2;
  int val = 1;
  vector< vector<int> > v(rows, vector<int> (cols, val));  /*creates 2d vector “v[rows][cols]” and initializes all elements to “val == 1” (default value is 0)*/
  v[0][0] = 5;
  v[1][1] = 4;
  cout << v[0][0] << endl; //Output: 5cout << v[1][0] << endl; //Output: 1return 0;}
Posted by: Guest on June-23-2020
1

how to create 2d array using vector in c++

vector<vector<int>> vec(N, vector<int> (M, INT_MAX));

Explanation::
vector<vector<int>> -- will take the formed container
N -- Think like row of 2d Matrix
vector<int> (M, INT_MAX) -- In each row, there is again a vector associated with it, 
that will formed 2d array.
Posted by: Guest on July-13-2021

Browse Popular Code Answers by Language