2017-08-14 81 views
4

C++中是否有任何方法,它会根据对值的差异对一对向量进行排序。作为一个例子,假设我有4双向量对通过对元素的差异进行排序

1 3, 
5 6, 
2 3, 
12 5, 

如此,对差异是2 1 1 7,如果我按降序排序的排序矢量将是,

12 5, 
1 3, 
5 6, 
2 3, 

希望你明白我的问题是什么。有什么方法可以用这种方法对元素进行排序吗?

我试过这种方法来根据第一个或第二个元素对元素进行排序。但这不是我的问题。我的问题是我需要根据差异进行排序。

bool sortinrev(const pair<int,int> &a, const pair<int,int> &b){ 
    return(a.first > b.first) ; 
} 


int main() 
{ 
    vector< pair <int,int> > pq; 
    for(int i=1; i<=4; i++){ 
     int x,y; 
     cin >> x >> y; 

     pq.push_back(make_pair(x,y)); 
    } 

    sort(pq.begin(), pq.end(), sortinrev); 

    for(int i=0; i<4; i++){ 
     cout << pq[i].first << " " << pq[i].second << endl; 
    } 


    return 0; 
} 
+3

的[排序函数(http://www.cplusplus.com/reference/algorithm/sort/)C++有可能需要一个函数指针作为参数。只要传递一个函数,按照你想要的方式比较对。 – scohe001

+0

我只知道如何根据第一个元素或基于第二个元素对它进行排序。由于我不知道如何根据差异进行排序,所以我贴出来了解方式。然后我会尝试解决我的实际问题。 –

+0

您可以将'compare'变量传递给'sort()'方法。请参阅@ scohe001在他的评论中的链接。 –

回答

8

如果你的容器是

std::vector<std::pair<int, int>> data; 

你可以把它作为

std::sort(std::begin(data), 
      std::end(data), 
      [](std::pair<int, int> const& lhs, std::pair<int, int> const& rhs) 
      { 
       return std::abs(lhs.first - lhs.second) < std::abs(rhs.first - rhs.second); 
      }); 

如果你想升序和降序只需切换从<相应>之间切换排序。

+3

你的代码对于这种情况是完美的,但如果OP不知道如何查找并找到这种排序函数,我怀疑他会理解lambda表达式。也许添加一个解释?或者让比较器具有自己的功能? – scohe001

1

std :: sort有一个需要调用比较的重载。

template< class RandomIt, class Compare > 
void sort(RandomIt first, RandomIt last, Compare comp); 

所以,你可以通过一个lambda(或其他功能)作为tcompares任何你想要的方式,第三个参数。

从cppreference.com:

comp  - comparison function object (i.e. an object that satisfies the  requirements of Compare) which returns ​true if the first argument is less than (i.e. is ordered before) the second. 
The signature of the comparison function should be equivalent to the following: 

bool cmp(const Type1 &a, const Type2 &b); 

The signature does not need to have const &, but the function object must not modify the objects passed to it. 
The types Type1 and Type2 must be such that an object of type RandomIt can be dereferenced and then implicitly converted to both of them. ​ 

因此,例如

sort(begin(myvect), end(myvect), [](auto p) { /* comparison code */ }); 

(需要C++ 14,您可能需要修改,取决于你的编译器版本)

2

标准库提供了一个数据结构std::pair和一个排序算法0​​,您可以向其传递定制比较定义的顺序。请参阅下面的代码,该代码定义了一个比较器,它取两个std::pair<int,int>并根据它们的“绝对差异”将它们进行比较,以及代码如何调用std::sort。希望能帮助到你。

#include <iostream> 
#include <vector> 

int main() 
{ 
    std::vector<std::pair<int,int> > v = { 
     {1, 3}, 
     {5, 6}, 
     {2, 3}, 
     {12, 5} 
    }; 

    // sort using a custom function object 
    struct { 
     bool operator()(const std::pair<int,int> &a, const std::pair<int,int> &b) const 
     { 
      return (abs(a.first-a.second) > abs(b.first-b.second)); 
     } 
    } differenceIsGreater; 
    std::sort(v.begin(), v.end(), differenceIsGreater); 
    for (auto a : v) { 
     std::cout << "(" << a.first << "," << a.second << ")" << std::endl; 
    } 

    return 0; 
} 

输出:

(12,5) 
(1,3) 
(5,6) 
(2,3) 
相关问题