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 vector cpp

For user defined size:

vector<vector<int>> vec( n ); 
where n -> number of rows

vector<vector<int>> vec( n , vector<int> (m, 0)); 
where n -> number of rows
where m -> number of columns (all initialized with 0)
  
Comment

2D vector in cpp

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

2D vector in cpp

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

PREVIOUS NEXT
Code Example
Cpp :: allow cross origin 
Cpp :: roscpp publish int32 
Cpp :: strip space from string cpp 
Cpp :: c++ find element in vector 
Cpp :: c++ switch case break 
Cpp :: http.begin() error 
Cpp :: how to round to nearest whole number unity 
Cpp :: iterating in map/unordered map c++ 
Cpp :: c++ printf char as hex 
Cpp :: string reverse stl 
Cpp :: C++ Volume of a Sphere 
Cpp :: how print fload wiht 2 decimal in c++ 
Cpp :: c++ 2d vector assign value 
Cpp :: opencv c++ image write 
Cpp :: cpp create multidimensional vector 
Cpp :: c++ cout colored output xcode 
Cpp :: convert string to lpstr 
Cpp :: convert integer to string c++ 
Cpp :: the code execution cannot proceed because glew32.dll was not found 
Cpp :: combine two vectors c++ 
Cpp :: size of array 
Cpp :: declare nullptr c++ 
Cpp :: c++ array rev pointer 
Cpp :: int main() { 
Cpp :: overload of << c++ 
Cpp :: ascii conversion cpp 
Cpp :: chudnovsky algorithm c++ 
Cpp :: sleep in c++ 
Cpp :: sum of row s2 d array c++ 
Cpp :: pure virtual function in c++ 
ADD CONTENT
Topic
Content
Source link
Name
6+3 =