Home > Backend Development > C++ > How to Sort a Vector of Pairs in C Based on the Second Element?

How to Sort a Vector of Pairs in C Based on the Second Element?

Linda Hamilton
Release: 2024-12-16 21:31:12
Original
1003 people have browsed it

How to Sort a Vector of Pairs in C   Based on the Second Element?

Sorting a Vector of Pairs Based on the Second Element

In C , we can encounter situations where we need to sort a vector of pairs based on the second element. Let's explore how to achieve this effortlessly using the provided information.

The provided vector of pairs, vec, holds pairs of integers and we want to sort it in ascending order based on the second element of each pair. Without resorting to custom function objects, let's leverage the standard template library (STL) and std::less to do the job.

Using C 14 Lambdas

The easiest solution in C 14 leverages lambdas with parameter type inference:

std::sort(vec.begin(), vec.end(), [](auto &left, auto &right) {
    return left.second < right.second;
});
Copy after login

This anonymous lambda captures the left and right pairs and returns true if the second element of left is less than that of right, effectively sorting the vector in ascending order.

Using Custom Comparators

If lambdas are not an option, we can define a custom comparator to handle the sorting:

struct sort_pred {
    bool operator()(const std::pair<int, int> &left, const std::pair<int, int> &right) {
        return left.second < right.second;
    }
};

std::sort(vec.begin(), vec.end(), sort_pred());
Copy after login

The sort_pred struct implements the comparison operator, comparing the second elements of the two pairs and returning true if the left pair's element is less than the right.

Using a Template Comparator

To generalize the comparator and reuse it for sorting, we can create a template:

template <class T1, class T2, class Pred = std::less<T2>>
struct sort_pair_second {
    bool operator()(const std::pair<T1, T2> &left, const std::pair<T1, T2> &right) {
        Pred p;
        return p(left.second, right.second);
    }
};
Copy after login

With this template, we can now sort using:

std::sort(vec.begin(), vec.end(), sort_pair_second<int, int>());
Copy after login

By providing the template arguments, we specify the types of the pair elements and can even customize the comparison using a different predicate like std::greater for descending order.

The above is the detailed content of How to Sort a Vector of Pairs in C Based on the Second Element?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template