Razvrščanje 2D vektorja v C ++ | Set 3 (po številu stolpcev)
Razpravljali smo o nekaterih primerih razvrščanja 2D vektorja v spodnjem nizu 1 in nastavitvi 2.
Razvrščanje 2D vektorja v C ++ | Set 1 (po vrstici in stolpcu)
Razvrščanje 2D vektorja v C ++ | Set 2 (v padajočem vrstnem redu po vrstici in stolpcu)
V tem članku je obravnavanih več primerov
Kot je omenjeno v enem od članka, objavljenega iz tega sklopa, ima lahko 2D vektor tudi vrstice z različnim številom stolpcev. Ta lastnost je za razliko od 2D matrike, v katerem imajo vse vrstice enako število stolpcev.
// 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 & lt ; vector & lt ; int & gt ; & gt ; vect {{ 1 2 } { 3 4 5 } { 6 }}; // Displaying the 2D vector for ( int i = 0 ; i & lt ; vect . size (); i ++ ) { //loop till the size of particular //row for ( int j = 0 ; j & lt ; vect [ i ]. size () ; j ++ ) cout & lt ; & lt ; vect [ i ][ j ] & lt ; & lt ; & quot ; & quot ;; cout & lt ; & lt ; endl ; } return 0 ; }
Izhod:
1 2 3 4 5 6
Časovna kompleksnost: O (n*m) n je število vrstic in m je število stolpcev
Vesoljska kompleksnost: O (n*m)
Primer 5: razvrščanje 2D vektorja na podlagi št. stolpcev v vrsti v naraščajočem vrstnem redu.
Pri tej vrsti razvrščanja je 2D vektor razvrščen na podlagi št. stolpca v naraščajočem vrstnem redu. To dosežemo s tem, da se tretji argument prenaša v sorti () kot klic k uporabniški definirani izrecni funkciji.
// 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 & lt ; int & gt ; & amp ; v1 const vector & lt ; int & gt ; & amp ; v2 ) { return v1 . size () & lt ; v2 . size (); } int main () { // Initializing 2D vector 'vect' with // values vector & lt ; vector & lt ; int & gt ; & gt ; vect {{ 1 2 } { 3 4 5 } { 6 }}; // Displaying the 2D vector before sorting cout & lt ; & lt ; & quot ; The Matrix before sorting is : n & quot ;; for ( int i = 0 ; i & lt ; vect . size (); i ++ ) { //loop till the size of particular //row for ( int j = 0 ; j & lt ; vect [ i ]. size () ; j ++ ) cout & lt ; & lt ; vect [ i ][ j ] & lt ; & lt ; & quot ; & quot ;; cout & lt ; & lt ; 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 & lt ; & lt ; & quot ; The Matrix after sorting is : n & quot ;; for ( int i = 0 ; i & lt ; vect . size (); i ++ ) { //loop till the size of particular //row for ( int j = 0 ; j & lt ; vect [ i ]. size () ; j ++ ) cout & lt ; & lt ; vect [ i ][ j ] & lt ; & lt ; & quot ; & quot ;; cout & lt ; & lt ; endl ; } return 0 ; }
Izhod:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5
Časovna kompleksnost: O (nlog (n))
Vesoljska kompleksnost: O (n*m)
Primer 6: Razvrščanje 2D vektorja na podlagi št. stolpcev v vrsti v padajočem vrstnem redu.
Pri tej vrsti razvrščanja je 2D vektor razvrščen na podlagi št. stolpca v padajočem vrstnem redu. To dosežemo s tem, da se tretji argument prenaša v sorti () kot klic k uporabniški definirani izrecni funkciji.
// 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 & lt ; int & gt ; & amp ; v1 const vector & lt ; int & gt ; & amp ; v2 ) { return v1 . size () & gt ; v2 . size (); } int main () { // Initializing 2D vector 'vect' with // values vector & lt ; vector & lt ; int & gt ; & gt ; vect {{ 1 2 } { 3 4 5 } { 6 }}; // Displaying the 2D vector before sorting cout & lt ; & lt ; & quot ; The Matrix before sorting is : n & quot ;; for ( int i = 0 ; i & lt ; vect . size (); i ++ ) { //loop till the size of particular //row for ( int j = 0 ; j & lt ; vect [ i ]. size () ; j ++ ) cout & lt ; & lt ; vect [ i ][ j ] & lt ; & lt ; & quot ; & quot ;; cout & lt ; & lt ; 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 & lt ; & lt ; & quot ; The Matrix after sorting is : n & quot ;; for ( int i = 0 ; i & lt ; vect . size (); i ++ ) { //loop till the size of particular //row for ( int j = 0 ; j & lt ; vect [ i ]. size () ; j ++ ) cout & lt ; & lt ; vect [ i ][ j ] & lt ; & lt ; & quot ; & quot ;; cout & lt ; & lt ; endl ; } return 0 ; }
Izhod:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6
Časovna kompleksnost: O (nlog (n))
Vesoljska kompleksnost: O (n*m)