2D vector in C++ with user defined size

A 2D vector is a vector of vector. Like 2D arrays, we can declare and assign values to 2D matrix. 二维vector是由vector组成的vector,和二维数组一样,可以声明和定义二维数组。

// C++ code to demonstrate 2D vector 
#include <iostream> 
#include <vector> // for 2D vector 
using namespace std; 

int main() 
{ 
	// Initializing 2D vector "vect" with 
	// values 
	vector<vector<int> > vect{ { 1, 2, 3 }, 
							{ 4, 5, 6 }, 
							{ 7, 8, 9 } }; 

	// Displaying the 2D vector 
	for (int i = 0; i < vect.size(); i++) { 
		for (int j = 0; j < vect[i].size(); j++) 
			cout << vect[i][j] << " "; 
		cout << endl; 
	} 

	return 0; 
} 

Outout :

1 2 3 
4 5 6 
7 8 9 

Like Java’s jagged arrays, each row of 2D vector can contain different number of columns. 二维vector的行可以包含不同数量的列。

// C++ code to demonstrate 2D vector where 
// each row is of different size. 
#include <iostream> 
#include <vector> // for 2D vector 
using namespace std; 

int main() 
{ 
	// Initializing 2D vector "vect" with 
	// different number of values in each 
	// row. 
	vector<vector<int> > vect{ { 1, 2 }, 
							{ 4, 5, 6 }, 
							{ 7, 8, 9, 10 } }; 

	// Displaying the 2D vector 
	for (int i = 0; i < vect.size(); i++) { 
		for (int j = 0; j < vect[i].size(); j++) 
			cout << vect[i][j] << " "; 
		cout << endl; 
	} 

	return 0; 
} 

Output :

1 2 
4 5 6 
7 8 9 10 

Exercise Problem : Define the 2D vector with different sizes of column input by user.
Examples:

Input : Number of rows : 5 
        Number of columns in rows : 
        2 3 4 5 1
Output : 1 2
         1 2 3
         1 2 3 4
         1 2 3 4 5 
         1

Input : Number of rows : 3
        Number of columns in rows : 
        3 2 1
Input : 3 3 2 1
Output : 1 2 3
         1 2
         1

First we take the input of row and and take the input of column of every row. 
Now the initialize the memory of every row by the size of column.

// CPP program to create a 2D vector where 
// every row has 
#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 
	// size of row 
	int row = 5; 
	int colom[] = { 5, 3, 4, 2, 1 }; 

	// Create a vector of vector with size 
	// equal to row. 
	vector<vector<int> > vec(row); 

	for (int i = 0; i < row; i++) { 

		// size of column 
		int col; 
		col = colom[i]; 

		// declare the i-th row to size of column 
		vec[i] = vector<int>(col); 
		for (int j = 0; j < col; j++) 
			vec[i][j] = j + 1; 
	} 

	for (int i = 0; i < row; i++) { 
		for (int j = 0; j < vec[i].size(); j++) 
			cout << vec[i][j] << " "; 
		cout << endl; 
	} 
} 

Output:

1 2 3 4 5
1 2 3 
1 2 3 4
1 2
1
// C++ code to demonstrate 2D vector 
#include<iostream> 
#include<vector> // for 2D vector 
using namespace std; 

int main() 
{ 
	// Initializing 2D vector "vect" with 
	// values 
	int row;
	cin>>row;
	vector< vector<int> >vec(row);
	for(int i=0; i<row; i++){
		int col;
		cin>>col;
		vec[i] = vector<int>(col);
		for(int j=0; j<col; j++)
		    vec[i][j] = j+1;
	}
	for(int i=0; i<row; i++){
		for(int j=0; j<vec[i].size(); j++)
	        cout<<vec[i][j] << " ";
	    cout<<endl;
	}
	    
	return 0; 
} 

output:

5
4 3 2 1 6
1 2 3 4
1 2 3
1 2
1
1 2 3 4 5 6

This article is contributed by Amit Verma. If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to [email protected]. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above.

Recommended Posts:

猜你喜欢

转载自blog.csdn.net/qq_27009517/article/details/86520278