sort a 2d vector c++ stl

// C++ code to demonstrate sorting of a
// 2D vector on basis of a column
#include <algorithm> // for sort()
#include <iostream>
#include <vector> // for 2D vector
using namespace std;
 
// Driver function to sort the 2D vector
// on basis of a particular column
bool sortcol(const vector<int>& v1, const vector<int>& v2)
{
    return v1[1] < v2[1];
}
 
// Driver Code
int main()
{
    // Initializing 2D vector "vect" with
    // values
    vector<vector<int> > vect{ { 3, 5, 1 },
                               { 4, 8, 6 },
                               { 7, 2, 9 } };
 
    // Number of rows;
    int m = vect.size();
 
    // Number of columns (Assuming all rows
    // are of same size). We can have different
    // sizes though (like Java).
    int n = vect[0].size();
 
    // Displaying the 2D vector before sorting
    cout << "The Matrix before sorting is:\n";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }
 
    // Use of "sort()" for sorting on basis
    // of 2nd column
    sort(vect.begin(), vect.end(), sortcol);
 
    // Displaying the 2D vector after sorting
    cout << "The Matrix after sorting is:\n";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }
    return 0;
}

c++ sort a 2d vector by column

// C++ code to demonstrate sorting of a
// row of 2D vector
#include <algorithm> // for sort()
#include <iostream>
#include <vector> // for 2D vector
using namespace std;
 
// Driver Code
int main()
{
    // Initializing 2D vector "vect" with
    // values
    vector<vector<int> > vect{ { 3, 5, 1 },
                               { 4, 8, 6 },
                               { 7, 2, 9 } };
    // Number of rows;
    int m = vect.size();
 
    // Number of columns (Assuming all rows
    // are of same size). We can have different
    // sizes though (like Java).
    int n = vect[0].size();
 
    // Displaying the 2D vector before sorting
    cout << "The Matrix before sorting 1st row is:\n";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }
 
    // Use of "sort()" for sorting first row
    sort(vect[1].begin(), vect[1].end());
 
    // Displaying the 2D vector after sorting
    cout << "The Matrix after sorting 1st row is:\n";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }
 
    return 0;
}

c++ sort a 2d vector by column

The Matrix before sorting 1st row is:
3 5 1 
4 8 6 
7 2 9 
The Matrix after sorting 1st row is:
1 3 5 
4 8 6 
7 2 9

C++相关代码片段

how to kill

hello world cpp

cpp hello world

when kotlin

how to write hello world c++

fenwick tree

main function

python loop

is palindrom

subsequence

insertion sort

react cookie

data structure

Stack Modified

increment integer

496. Next Greater Element I.cpp

c++ freecodecamp course 10 hours youtube

simple interest rate

deliberation meaning

fingerprint

c++ system() from variable

operator using

unambiguous

Stream Overloading

quick_sort

hash table

graph colouring

the question for me

fname from FString

how togreper

is plaindrome

logisch oder

robot framework for loop example

spyder enviroment

pallindrome string

ue4 c++ string from fvector

interactive problem

two sum solution

interpolation search

Required Length

new expression

Targon lol

xor linked list

stack implementation

typescript

loop through array

loop array

how to point to next array using pointer c++

file exist

floating point exception

array di struct

castin in C++

varint index

how to make a instagram report bot python

windows servis from console app

add integers

listas enlazadas/ linked lists

niet werkend

bubble sort

triangle angle sum

divisor summation

rotateArray

destiny child

Populating Next Right Pointers in Each Node

cosnt cast

bucket sort

double plus overload

Z-function

binary search

permutation

linked list

Implicit conversion casting

square root

public method

tower of hanoi

selection sort

dangling pointer

hello world programming

statements

volumeof a sphere