Diskutovali sme o niektorých prípadoch triedenia 2D vektora v nižšie uvedenej sade 1 a set 2.
Triedenie 2D vektor v C ++ | Sada 1 (po riadku a stĺpci)
Triedenie 2D vektor v C ++ | Set 2 (v zostupnom poradí po riadku a stĺpci)
V tomto článku sa diskutuje viac prípadov
Ako je uvedené v jednom z článku uverejneného tohto súboru, 2D vektor môže mať tiež riadky s rôznym počtom stĺpcov. Táto vlastnosť je na rozdiel od 2D poľa, v ktorom majú všetky riadky rovnaký počet stĺpcov.
// 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; }
Výstup:
1 2 3 4 5 6
Časová zložitosť: O (n*m) n je počet riadkov a m je počet stĺpcov
Zložitosť vesmíru: O (n*m)
Prípad 5: Triedenie 2D vektora na základe č. stĺpcov v rade vo vzostupnom poradí.
V tomto type triedenia je 2D vektor triedený na základe č. stĺpca vo vzostupnom poradí. To sa dosiahne odovzdaním tretieho argumentu v Sort () ako volania na explicitnú funkciu definovanú používateľom.
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; }
Výstup:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5
Časová zložitosť: O (nlog (n))
Zložitosť vesmíru: O (n*m)
Prípad 6: Triedenie 2D vektora na základe č. stĺpcov v riadku v zostupnom poradí.
V tomto type triedenia je 2D vektor triedený na základe č. stĺpca v zostupnom poradí. To sa dosiahne odovzdaním tretieho argumentu v Sort () ako volania na explicitnú funkciu definovanú používateľom.
// 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; }
Výstup:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6
Časová zložitosť: O (nlog (n))
Zložitosť vesmíru: O (n*m)