Am discutat unele dintre cazurile de sortare a vectorului 2D în setul 1 de mai jos și setul 2.
Sortarea vectorului 2D în C ++ | Set 1 (prin rând și coloană)
Sortarea vectorului 2D în C ++ | Set 2 (în ordine descendentă prin rând și coloană)
Mai multe cazuri sunt discutate în acest articol
După cum am menționat într -unul din articolul publicat din acest set, un vector 2D poate avea și rânduri cu un număr diferit de coloane. Această proprietate este spre deosebire de tabloul 2D în care toate rândurile au același număr de coloane.
// 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; }
Ieșire:
1 2 3 4 5 6
Complexitate a timpului: O (n*m) n este numărul de rânduri și m este numărul de coloane
Complexitate spațială: O (n*m)
Cazul 5: Sortarea vectorului 2D pe baza nr. de coloane în rând în ordine ascendentă.
În acest tip de sortare a vectorului 2D este sortat pe baza unui nr. de coloană în ordine ascendentă. Acest lucru se realizează prin trecerea unui al treilea argument în sortare () ca un apel către funcția explicită definită de utilizator.
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; }
Ieșire:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5
Complexitate a timpului: O (nlog (n))
Complexitate spațială: O (n*m)
Cazul 6: Sortarea vectorului 2D pe baza nr. de coloane în rând în ordine descendentă.
În acest tip de sortare a vectorului 2D este sortat pe baza unui nr. de coloană în ordine descendentă. Acest lucru se realizează prin trecerea unui al treilea argument în sortare () ca un apel către funcția explicită definită de utilizator.
// 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; }
Ieșire:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6
Complexitate a timpului: O (nlog (n))
Complexitate spațială: O (n*m)