What is a 2D Vector?
A 2D vector is vector of vectors. It is an matrix implemented with the help of vectors. 二维的vector,是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;
}
Output:
1 2 3 4 5 6 7 8 9
Case 1 : To sort a particular row of 2D vector. 对二维的vector的特定行进行排序。
This type of sorting arranges a selected row of 2D vector in ascending order . This is achieved by using “sort()” and passing iterators of 1D vector as its arguments. 这种排序对二维vector中的特定的行进行升序排序。用函数sort()实现,传一维vector的迭代器作为参数。
// C++ code to demonstrate sorting of a
// row of 2D vector
#include<iostream>
#include<vector> // for 2D vector
#include<algorithm> // for sort()
using namespace std;
int main()
{
// Initializing 2D vector "vect" with
// values
vector< vector<int> > vect{
{3, 5, 1},
{4, 8, 6},
{7, 2, 9}};
// Number of rows;
int m = vect.size();
// Number of columns (Assuming all rows
// are of same size). We can have differ