简体   繁体   English

stable_sort 按对中的第一个元素在对的向量上按升序排列,没有比较器 C++

[英]stable_sort on a vector of pairs by first element in pair in increasing order without comparator function in C++

#include <bits/stdc++.h> 
using namespace std; 

int main() 
{ 

    vector<pair<int,int>>v;
    v.push_back(make_pair(1,3));
    v.push_back(make_pair(1,1));
    v.push_back(make_pair(2,19));
    v.push_back(make_pair(2,4));

    int n = 4; 

    stable_sort(v.begin(),v.end()); 

    for (int i = 0; i < n; i++) 
        cout << "[" << v[i].first << ", " << v[i].second 
             << "] "; 

    return 0; 
} 

Output: [1, 1] [1, 3] [2, 4] [2, 19] Expected Output: [1, 3] [1, 1] [2, 19] [2, 4] Output: [1, 1] [1, 3] [2, 4] [2, 19]预期 Output: [1, 3] [1, 1] [2, 19] [2, 4]

Why does the vector of pairs not maintain the relative ordering even after applying stable sort, when we know that by default the vector of pairs is sorted on the basis of first element of the vector?当我们知道默认情况下,对的向量是根据向量的第一个元素进行排序时,为什么即使在应用稳定排序之后,对的向量也不能保持相对排序? If I write the comparator function, then it is working properly but not when I don't define any comparator function.如果我写比较器 function,那么它工作正常,但当我没有定义任何比较器 function 时它就不能正常工作。 Why is it so?为什么会这样?

The comparator used uses both values in the pairs when doing the comparison.在进行比较时,使用的比较器使用对中的两个值。

It compares lhs and rhs lexicographically by operator< ( operator<=> since C++20), that is, compares the first elements and only if they are equivalent, compares the second elements.它通过operator< (从 C++20 开始为operator<=> )按字典顺序比较 lhs 和 rhs,也就是说,比较第一个元素,只有当它们等价时,才比较第二个元素。

If I write the comparator function, then it is working properly如果我写比较器 function,那么它工作正常

That's because you've probably implemented it like this:那是因为您可能已经像这样实现它:

[](const auto& lhs, const auto& rhs) {
    return lhs.first < rhs.first;
}

The default comparator does something equivalent to this:默认比较器执行与此等效的操作:

[](const auto& lhs, const auto& rhs) {
    return lhs.first == rhs.first ? lhs.second < rhs.second : lhs.first < rhs.first;
}

The reference for operator< of std::pair<int, int> says that it std::pair<int, int>operator<参考说它

Compares lhs and rhs lexicographically by operator<, that is, compares the first elements and only if they are equivalent, compares the second elements.通过 operator< 按字典顺序比较 lhs 和 rhs,即比较第一个元素,只有当它们相等时,才比较第二个元素。

So both elements are used to compare a std::pair and what you see is a fully sorted vector.因此,这两个元素都用于比较std::pair并且您看到的是完全排序的向量。 Relative order of elements would only be relevant when two or more elements can be considered equal.只有当两个或多个元素可以被认为是相等的时,元素的相对顺序才相关。 Since both values of the pairs are used for comparison none of them can be considered equal.由于这对的两个值都用于比较,因此它们中的任何一个都不能被认为是相等的。 Thus relative order is not relevant here.因此相对顺序在这里不相关。

You can use a custom comparator to only compare the first element:您可以使用自定义比较器仅比较第一个元素:

stable_sort(v.begin(), v.end(), [](const auto& a, const auto& b) {return a.first < b.first; });

An will see the output安会看到output

[1, 3] [1, 1] [2, 19] [2, 4]

Here the first two and last two pairs are considered equal and keep their relative order.这里前两对和最后两对被认为是相等的并保持它们的相对顺序。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM