Answers for "how to insert in 2d vector c++"

C++
1

c++ 2d vector assign value

vector<vector<int>> v;
   int val;
   int n = size(v);
   for(int i = 0; i < n; i++)
{
      vector<int> temp;
      for(int j = 0; j < n; j++){
         cin >> val;
      temp.push_back(val);
      }
      v.push_back(temp);
      temp.clear();
Posted by: Guest on November-06-2021
9

how to make a 2d vector in c++

// Create a vector containing n 
//vectors of size m, all u=initialized with 0
vector<vector<int> > vec( n , vector<int> (m, 0));
Posted by: Guest on June-19-2020
0

create a 2d vector in c++

// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n = 3;
    int m = 4;
 
    /*
    We create a 2D vector containing "n"
    elements each having the value "vector<int> (m, 0)".
    "vector<int> (m, 0)" means a vector having "m"
    elements each of value "0".
    Here these elements are vectors.
    */
    vector<vector<int>> vec( n , vector<int> (m, 0));
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout<< endl;
    }
     
    return 0;
}
Posted by: Guest on March-09-2022

Browse Popular Code Answers by Language