Vector de clasificación de pares en C++ | Conjunto 2 (Ordenar en orden descendente por primero y segundo)

Hemos discutido algunos de los casos de clasificación de vectores de pares en el siguiente conjunto 1.
Clasificación de vectores de pares en C++ | Conjunto 1 (Ordenar por primero y segundo)
Más casos se discuten en este artículo
A veces necesitamos ordenar el vector en orden inverso. En esos casos, en lugar de ordenar primero el vector y luego usar la función «inversa», aumenta la complejidad del tiempo del código. Por lo tanto, para evitar esto, ordenamos el vector directamente en orden descendente.
Caso 3: Clasificación de los elementos del vector sobre la base del primer elemento de pares en orden descendente.
Este tipo de clasificación organiza filas seleccionadas de pares en vector en orden descendente. Esto se logra usando «sort()» y pasando iteradores de vector 1D como sus argumentos.

CPP

// C++ program to demonstrate sorting in vector of
// pair according to 1st element of pair in
// descending order
#include<bits/stdc++.h>
using namespace std;
 
int main()
{
    // declaring vector of pairs
    vector< pair <int,int> > vect;
 
    // initializing 1st and 2nd element of
    // pairs with array values
    int arr[] = {5, 20, 10, 40 };
    int arr1[] = {30, 60, 20, 50};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    // Entering values in vector of pairs
    for (int i=0; i<n; i++)
        vect.push_back( make_pair(arr[i],arr1[i]) );
 
    // Printing the original vector(before sort())
    cout << "The vector before applying sort is:\n" ;
    for (int i=0; i<n; i++)
    {
        // "first" and "second" are used to access
        // 1st and 2nd element of pair respectively
        cout << vect[i].first << " "
             << vect[i].second << endl;
 
    }
 
    // using modified sort() function to sort
    sort(vect.rbegin(), vect.rend());
 
    // Printing the sorted vector(after using sort())
    cout << "The vector after applying sort is:\n" ;
    for (int i=0; i<n; i++)
    {
        // "first" and "second" are used to access
        // 1st and 2nd element of pair respectively
        cout << vect[i].first << " "
             << vect[i].second << endl;
    }
    return 0;
}

Producción: 
 

The vector before applying sort is:
5 30
20 60
10 20
40 50
The vector after applying sort is:
40 50
20 60
10 20
5 30



Caso 4: Clasificación de los elementos del vector sobre la base del segundo elemento de pares en orden descendente.
Estas instancias también se pueden manejar modificando la función «ordenar()» y nuevamente pasando una llamada a la función definida por el usuario.
 

CPP

// C++ program to demonstrate sorting/in vector of
// pair according to 2nd element of pair in
// descending order
#include<bits/stdc++.h>
using namespace std;
 
// Driver function to sort the vector elements by
// second element of pair in descending order
bool sortbysecdesc(const pair<int,int> &a,
                   const pair<int,int> &b)
{
       return a.second>b.second;
}
 
 
int main()
{
    // Declaring vector of pairs
    vector< pair <int,int> > vect;
 
    // Initializing 1st and 2nd element of
    // pairs with array values
    int arr[] = {5, 20, 10, 40 };
    int arr1[] = {30, 60, 20, 50};
    int n = sizeof(arr)/sizeof(arr[0]);
 
    // Entering values in vector of pairs
    for (int i=0; i<n; i++)
        vect.push_back( make_pair(arr[i],arr1[i]) );
 
    // Printing the original vector(before sort())
    cout << "The vector before sort operation is:\n" ;
    for (int i=0; i<n; i++)
    {
        // "first" and "second" are used to access
        // 1st and 2nd element of pair respectively
        cout << vect[i].first << " "
            << vect[i].second << endl;
    }
 
    // using modified sort() function to sort
    sort(vect.begin(), vect.end(), sortbysecdesc);
 
    // Printing the sorted vector(after using sort())
    cout << "The vector after applying sort operation is:\n" ;
    for (int i=0; i<n; i++)
    {
        // "first" and "second" are used to access
        // 1st and 2nd element of pair respectively
        cout << vect[i].first << " "
             << vect[i].second << endl;
    }
    return 0;
}

Producción: 
 

The vector before sort operation is:
5 30
20 60
10 20
40 50
The vector after applying sort operation is:
20 60
40 50
5 30
10 20



Este artículo es una contribución de Manjeet Singh . Si le gusta GeeksforGeeks y le gustaría contribuir, también puede escribir un artículo usando contribuya.geeksforgeeks.org o envíe su artículo por correo a contribuya@geeksforgeeks.org. Vea su artículo que aparece en la página principal de GeeksforGeeks y ayude a otros Geeks.
Escriba comentarios si encuentra algo incorrecto o si desea compartir más información sobre el tema tratado anteriormente.
 

Publicación traducida automáticamente

Artículo escrito por GeeksforGeeks-1 y traducido por Barcelona Geeks. The original can be accessed here. Licence: CCBY-SA

Deja una respuesta

Tu dirección de correo electrónico no será publicada. Los campos obligatorios están marcados con *