Vector In C++ With User Defined Size

This tutorial aims to explain the concept of a 2D vector with a user-defined size. We must be aware of 2d arrays where the array is two-dimensional, which can be visualized as a matrix.

Here the concept of vectors solves the central pain point of fixed-size collections, wherein the idea of vectors is dynamic.

A 2D vector is simply a vector of a vector and is involved in the heart. The time and space complexities of a 2D vector are O(1).

Approach 1:

C++ Code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include <vector>
using namespace std;
// The main driver code functionality starts from here
int main()
{
	
/*  theory behind vector implementation
	
	1. vector<datatype> variable_name (in case of a 2d vector)
	
	In the case of a 2D vector
	
	All we do is create
	
	A vector of datatype vector.
	
	We replace "datatype" with "vector<int>":
	
	1. vector<vector<int>> variable_name (simple change to create a vector)
	
*/
	
	vector<vector<int>> vect;

	return 0;
// the main driver code functionality ends from here
}

Approach 2:

C++ code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include <iostream>
#include <vector>
using namespace std;
// The main driver code functionality starts from here
int main()
{
	
	// In the below code, we have tried to initialize a 2D vector
	// named "vect" on line 12 and then
	// we declare the values on
	// lines code respectively.
	
	
	vector<vector<int>> vect
	{
		{11, 2, 33},
		{74, 5, 66},
		{7, 98, 9}
	};
	
	// Now, from the below code, we are trying to print the values that
	// we just declared on lines
	// code using a simple
	// nested for loop.
	
	for (int i = 0; i < vect.size(); i++)
	{
	    // this is inner for loop to implement the vector code 
	    // functionality
		for (int j = 0; j < vect[i].size(); j++)
		{
			cout << vect[i][j] << " ";
		}	
		cout << endl;
	}

	return 0;
	// the main driver code functionality ends from here
}

Output:

Output

11 2 33 
74 5 66 
7 98 9

Approach 3:

C++ code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include <iostream>
#include <vector>
using namespace std;
// The main driver code functionality starts from here
int main()
{
	int n = 4;
	int m = 5;

	
//  here, we are trying to create a vector that contains the variable "n."
//	and a vector of functionality each of size "m."

	vector<vector<int>> vec( n , vector<int> (m));
// for loop to run through the variables n to m
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			vec[i][j] = j + i + 1;
		}
	}
// for loop to run through the variables m to n
	for(int i = 0; i < n; i++)
	{
		for(int j = 0; j < m; j++)
		{
			cout << vec[i][j] << " ";
		}
		cout << endl;
	}
	
return 0;
// The main driver code functionality ends from here
}

Output:

Output

1 2 3 4 5 
2 3 4 5 6 
3 4 5 6 7 
4 5 6 7 8

Approach 4:

C++ code

Example

// Here we are writing down the C++ programming language code to demonstrate
// the concept of 2D Vector In C++ With User Defined Size with its relevant 
// C++ programming language code and its output
#include <iostream>
#include <vector>
using namespace std;
// The main driver code functionality starts from here
int main()
{
    // below are the limits of variables n and m limits to loop
	int n = 3;
	int m = 4;

	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;
	// The main driver code functionality ends from here
}

Output:

Output

0 0 0 0 
0 0 0 0 
0 0 0 0

Input Required

This code uses input(). Please provide values below: