Olemme keskustelleet joistakin tapauksista 2D -vektorin lajittelusta alla olevassa sarjassa 1 ja 2.
2D -vektorin lajittelu C ++: ssa | Aseta 1 (rivillä ja sarakkeella)
2D -vektorin lajittelu C ++: ssa | Aseta 2 (laskevassa järjestyksessä rivin ja sarakkeen mukaan)
Tässä artikkelissa käsitellään enemmän tapauksia
Kuten yhdessä tämän sarjan julkaisemasta artikkelista mainitaan, 2D -vektorilla voi olla myös rivejä, joissa on erilainen sarakkeiden määrä. Tämä ominaisuus on toisin kuin 2D -taulukko, jossa kaikissa riveissä on sama määrä sarakkeita.
säännöllinen lauseke javassaCPP
// 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; }
Lähtö:
1 2 3 4 5 6
Ajan monimutkaisuus: O (n*m) n on rivien lukumäärä ja m on sarakkeiden lukumäärä
Avaruuden monimutkaisuus: O (n*m)
Tapaus 5: 2D -vektorin lajittelu nro. sarakkeet rivissä nousevassa järjestyksessä.
Tämän tyyppisessä lajittelu 2D -vektori on lajiteltu NO: n perusteella. sarakkeesta nousevassa järjestyksessä. Tämä saavutetaan siirtämällä kolmas argumentti Sort () -puheluna käyttäjän määrittelemälle nimenomaiselle toiminnolle.
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; }
Lähtö:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5
Ajan monimutkaisuus: O (nlog (n))
Avaruuden monimutkaisuus: O (n*m)
Tapaus 6: 2D -vektorin lajittelu ei. sarakkeet rivillä laskevassa järjestyksessä.
Tämän tyyppisessä lajittelu 2D -vektori on lajiteltu NO: n perusteella. sarakkeesta laskevassa järjestyksessä. Tämä saavutetaan siirtämällä kolmas argumentti Sort () -puheluna käyttäjän määrittelemälle nimenomaiselle toiminnolle.
// 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; }
Lähtö:
internetin käyttöä
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6
Ajan monimutkaisuus: O (nlog (n))
Avaruuden monimutkaisuus: O (n*m)