// Create a vector containing n
//vectors of size m, all u=initialized with 0
vector<vector<int> > vec( n , vector<int> (m, 0));
vector< vector<int>> a(rows, vector<int> (cols));
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)
// 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;
}
int a[2][3]= {
{1, 2, 3},
{4, 5, 6}
};
cout << a[1][1]; // Output is 5
// 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;
}
// 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;
}
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.
//* 2D arrays
int n, m;
cout << "Enter row and column: ";
cin >> n >> m;
int arr[n][m];
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cin >> arr[i][j];
}
}
cout << "
Showing array
";
for (int i = 0; i < n; i++)
{
for (int j = 0; j < m; j++)
{
cout << arr[i][j] << " ";
}
cout << endl;
}
Code Example |
---|
Cpp :: c++ check if string is isogram |
Cpp :: c++ string find example |
Cpp :: remove first occurrence of value from vector c++ |
Cpp :: sum of row s2 d array c++ |
Cpp :: Find the biggest element in the array |
Cpp :: compute power of number |
Cpp :: Convert a hexadecimal number into decimal c++ |
Cpp :: insertion sort cpp |
Cpp :: operand-- c++ |
Cpp :: sum of a matrix c++ |
Cpp :: dynamic memory c++ |
Cpp :: c++ initialize static variable |
Cpp :: how to say hello world in c++ |
Cpp :: c++ #define |
Cpp :: convert char to int c++ |
Cpp :: factorial of large number |
Cpp :: how to format decimal palces in c++ |
Cpp :: how to write a template c++ |
Cpp :: what do you mean by smallest anagram of a string |
Cpp :: standard template library in c++ |
Cpp :: CRED Coins codechef solution in c++ |
Cpp :: Translation codeforces in c++ |
Cpp :: C++ linked list iterator |
Cpp :: power in c++ |
Cpp :: linear search |
Cpp :: c++ custom hash |
Cpp :: c++ queue |
Cpp :: c++ - |
Cpp :: pointers c++ |
Cpp :: c++ create function pointer |