Hem discutit alguns dels casos d’ordenació del vector 2D al conjunt 1 i el conjunt 2.
Ordenació del vector 2D a C ++ | Set 1 (per fila i columna)
Ordenació del vector 2D a C ++ | Set 2 (en ordre descendent per fila i columna)
Es discuteixen més casos en aquest article
Com s'ha esmentat en un dels articles publicats d'aquest conjunt, un vector 2D també pot tenir files amb un nombre diferent de columnes. Aquesta propietat és diferent a la matriu 2D en què totes les files tenen el mateix nombre de columnes.
scan.next javaCPP
// C++ code to demonstrate 2D Vector // with different no. of columns #include #include // for 2D vector using namespace std; int main() { // Initializing 2D vector 'vect' with // values vector< vector<int> > vect{{1 2} {3 4 5} {6}}; // Displaying the 2D vector for (int i=0; i<vect.size(); i++) { //loop till the size of particular //row for (int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } return 0; }
Sortida:
1 2 3 4 5 6
Complexitat del temps: O (n*m) n és el nombre de files i m és el nombre de columnes
Complexitat espacial: O (n*m)
Cas 5: Ordenació del vector 2D a partir del núm. de columnes en fila en ordre ascendent.
En aquest tipus d’ordenació del vector 2D s’ordena a partir d’un núm. de columna en ordre ascendent. Això s’aconsegueix passant un tercer argument a Sort () com a trucada a la funció explícita definida per l’usuari.
CPP
// C++ code to demonstrate sorting of // 2D vector on basis of no. of columns // in ascending order #include #include // for 2D vector #include // for sort() using namespace std; // Driver function to sort the 2D vector // on basis of a no. of columns in // ascending order bool sizecom(const vector<int>& v1 const vector<int>& v2) { return v1.size() < v2.size(); } int main() { // Initializing 2D vector 'vect' with // values vector< vector<int> > vect{{1 2} {3 4 5} {6}}; // Displaying the 2D vector before sorting cout << "The Matrix before sorting is:n"; for (int i=0; i<vect.size(); i++) { //loop till the size of particular //row for (int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } //Use of 'sort()' for sorting on //basis of no. of columns in //ascending order. sort(vect.begin() vect.end() sizecom); // Displaying the 2D vector after sorting cout << "The Matrix after sorting is:n"; for (int i=0; i<vect.size(); i++) { //loop till the size of particular //row for (int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } return 0; }
Sortida:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5
Complexitat del temps: O (nlog (n))
Complexitat espacial: O (n*m)
Cas 6: Ordenació del vector 2D a partir del núm. de columnes en fila en ordre descendent.
En aquest tipus d’ordenació del vector 2D s’ordena a partir d’un núm. de columna en ordre descendent. Això s’aconsegueix passant un tercer argument a Sort () com a trucada a la funció explícita definida per l’usuari.
// C++ code to demonstrate sorting of // 2D vector on basis of no. of columns // in descending order #include #include // for 2D vector #include // for sort() using namespace std; // Driver function to sort the 2D vector // on basis of a no. of columns in // descending order bool sizecom(const vector<int>& v1 const vector<int>& v2) { return v1.size() > v2.size(); } int main() { // Initializing 2D vector 'vect' with // values vector< vector<int> > vect{{1 2} {3 4 5} {6}}; // Displaying the 2D vector before sorting cout << "The Matrix before sorting is:n"; for (int i=0; i<vect.size(); i++) { //loop till the size of particular //row for (int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } //Use of 'sort()' for sorting on //basis of no. of columns in //descending order. sort(vect.begin() vect.end() sizecom); // Displaying the 2D vector after sorting cout << "The Matrix after sorting is:n"; for (int i=0; i<vect.size(); i++) { //loop till the size of particular //row for (int j=0; j<vect[i].size() ;j++) cout << vect[i][j] << " "; cout << endl; } return 0; }
Sortida:
marquesina html
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6
Complexitat del temps: O (nlog (n))
Complexitat espacial: O (n*m)