Search
 
SCRIPT & CODE EXAMPLE
 

CPP

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:
";
    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:
";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }
    return 0;
}
Comment

sort 2d vector c++

std::sort(vector1.begin(),
          vector1.end(),
          [] (const std::vector<double> &a, const std::vector<double> &b)
          {
              return a[3] < b[3];
          });
Comment

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:
";
    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:
";
    for (int i = 0; i < m; i++) {
        for (int j = 0; j < n; j++)
            cout << vect[i][j] << " ";
        cout << endl;
    }
 
    return 0;
}
Comment

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
Comment

PREVIOUS NEXT
Code Example
Cpp :: are maps sorted c++ 
Cpp :: multiply two arbitrary integers a and b (a greater than b) 
Cpp :: inversed priority queue 
Cpp :: reference variablesr in c++ 
Cpp :: lcm recursive program in c++ 
Cpp :: template in cpp 
Cpp :: inverse lerp c++ 
Cpp :: Stack Modified 
Cpp :: c++ install 
Cpp :: c++ multi-dimensional arrays 
Cpp :: Determine if map contains a value for a key c++ 
Cpp :: c++ split string by sstream 
Cpp :: size of set c++ 
Cpp :: in c++ 
Cpp :: c++ filesystem remove file 
Cpp :: c++ delete int 
Cpp :: c++ generate random number upper and lower bound 
C :: how to use gotoxy in c language 
C :: conio.h linux 
C :: find maximum number between 3 numbers in c 
C :: sdl draw Rectf 
C :: how to get user input in c 
C :: arduino digital read 
C :: how to get add to number C 
C :: c convert integer to string 
C :: how to combine strings in c 
C :: Graphics in C Draw A Line 
C :: bitwise operators in c 
C :: how to make sure input is integer c 
C :: print a part of string c 
ADD CONTENT
Topic
Content
Source link
Name
4+5 =