C++ での 2D ベクトルの並べ替え |セット 3 (列数別)
以下のセット 1 とセット 2 で 2D ベクトルをソートするいくつかのケースについて説明しました。
C++ での 2D ベクトルの並べ替え |セット 1 (行と列ごと)
C++ での 2D ベクトルの並べ替え |セット 2 (行と列の降順)
この記事ではさらに多くのケースについて説明します
このセットについて公開された記事の 1 つで述べたように、2D ベクトルには異なる列数の行を含めることもできます。このプロパティは、すべての行が同じ数の列を持つ 2D 配列とは異なります。
// 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 ; }
出力:
1 2 3 4 5 6
時間計算量: O(n*m) n は行数、m は列数です。
空間の複雑さ: O(n*m)
ケース 5 : 番号に基づいて 2D ベクトルをソートする行内の列の昇順。
このタイプのソートでは、2D ベクトルが番号に基づいてソートされます。列の昇順。これは、sort() の 3 番目の引数をユーザー定義の明示的関数への呼び出しとして渡すことによって実現されます。
// 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 ; }
出力:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 6 1 2 3 4 5
時間計算量: O (nlog (n))
空間の複雑さ: O(n*m)
ケース 6 : 番号に基づいて 2D ベクトルをソートする行内の列の降順。
このタイプのソートでは、2D ベクトルが番号に基づいてソートされます。列の降順。これは、sort() の 3 番目の引数をユーザー定義の明示的関数への呼び出しとして渡すことによって実現されます。
// 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 ; }
出力:
The Matrix before sorting is: 1 2 3 4 5 6 The Matrix after sorting is: 3 4 5 1 2 6
時間計算量: O (nlog (n))
空間の複雑さ: O(n*m)