Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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));  
Comment

2d array using vector

// CPP program
#include <iostream>
#include <vector>
using namespace std;
int main()
{
    int n = 4;
    int m = 5;
 
    /*
    Create a vector containing "n"
    vectors each of size "m".
    */
    vector<vector<int>> vec( n , vector<int> (m));
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            vec[i][j] = j + i + 1;
        }
    }
 
    for(int i = 0; i < n; i++)
    {
        for(int j = 0; j < m; j++)
        {
            cout << vec[i][j] << " ";
        }
        cout << endl;
    }
     
   return 0;
}
Comment

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;
}
Comment

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.
Comment

PREVIOUS NEXT
Code Example
Cpp :: create a dictionary cpp 
Cpp :: c++ rule of five 
Cpp :: structure and function c++ 
Cpp :: c++ char array to int 
Cpp :: mpi_bcast 
Cpp :: how to find size of int array in c++ 
Cpp :: sort function descending c++ 
Cpp :: push front vector cpp 
Cpp :: bash test empty directory 
Cpp :: c++ map loop through key value 
Cpp :: default access modifier in c++ 
Cpp :: conditional operator in cpp 
Cpp :: find last occurrence of character in string c++ 
Cpp :: how to add colored text in c++ 
Cpp :: cpp list 
Cpp :: set precision with fixed c++ 
Cpp :: latex table landscape 
Cpp :: memcpy c++ usage 
Cpp :: height of bst cpp 
Cpp :: vector fin element c++ 
Cpp :: how to make a typing effect c++ 
Cpp :: convert string to lpstr 
Cpp :: matrix transpose in c++ 
Cpp :: array max and minimum element c++ 
Cpp :: matrix in vector c++ 
Cpp :: how to delete a file in cpp 
Cpp :: insert only unique values into vector 
Cpp :: continue statement in c++ program 
Cpp :: stoi() c++ 
Cpp :: bubblesort c++ 
ADD CONTENT
Topic
Content
Source link
Name
2+7 =