Search
 
SCRIPT & CODE EXAMPLE
 

CPP

how to sort a 2d array in c++

bool sortcol(const vector<int>& v1, const vector<int>& v2)
{
    return v1[1] < v2[1];
}
sort(vect.begin(), vect.end(), sortcol);
Comment

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

order 2d array in c++

9

The built-in arrays of C and C++ are very inflexible, among other things they cannot be assigned.

Your best option would be the 'array' class from the C++ standard library, at least for the inner dimension:

array<int, 2> a[5] = { { 20, 11 },
{ 10, 20 },
{ 39, 14 },
{ 29, 15 },
{ 22, 23 } };

sort( a, a + 5 );
Edit: Some more explanations.
Here we use the property of std::array that '<' by default compares them lexicographically, i.e. starts with the first element. In order to sort things differently we have to come up with an comparator object, so if you want to use the second column as sort key you have to do this:

auto comp = []( const array<int, 2>& u, const array<int, 2>& v )
      { return u[1] < v[1]; };
sort( a, a + 5, comp );
And as mentioned in the first comment, sort(a, a+5 ... is just an ugly short form for the cleaner sort(std::begin(a), std::end(a) ...
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 :: convert uppercase to lowercase 
Cpp :: replace a char in string c++ at a specific index 
Cpp :: pause the console c++ 
Cpp :: c++ allocate dynamic with initial values 
Cpp :: linkedlist in c++ 
Cpp :: sstream c++ 
Cpp :: c++ add everything in a vector 
Cpp :: cpprestsdk send file 
Cpp :: online converter c++ to c 
Cpp :: c ++ The output should be (abc),(def),(ghw) 
Cpp :: gtest assert not equal 
Cpp :: stack implementation 
Cpp :: creating large maps cpp 
Cpp :: how to find second smallest element in an array using single loop 
Cpp :: vector int initialize with increasing numbers 
Cpp :: #include <iostream #include <stdio.h using namespace std; int main() { int a[5]; a[0]=12; a[1]=13; a[2]=14; a[3]=15; 
Cpp :: the number of ones int bitset 
Cpp :: get range sum 
Cpp :: string in int in cpp 
Cpp :: why wont a function stop C++ 
Cpp :: passing reference to thread c++ 
Cpp :: stl map 
Cpp :: c++ click event 
Cpp :: qt get wireless interface name 
Cpp :: Buy 2 Get 1 Free codechef solution in c++ 
Cpp :: http://nv-study.ru/http://nv-study.ru/http://nv-study.ru/ 
Cpp :: hpp files 
Cpp :: quiz arrary and pointers in c++ 
Cpp :: initialize many variablles c++ 
Cpp :: sro in c++ 
ADD CONTENT
Topic
Content
Source link
Name
8+4 =