在c++中排序向量

Sorting vectors in c++

本文关键字:向量 排序 c++      更新时间:2023-10-16

我需要首先按sbp.second.second向量对数据结构vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp进行排序,并按sbp.second.second的相等值对sbp.second.first进行排序——这两个向量都通过(I)向量的大小进行比较;(ii)如果向量的大小相等,则向量按字典顺序排序。为此,我编写了以下代码。但我不知道为什么,但这段代码陷入了无限循环。有没有人能告诉我我哪里做错了?

#include <vector>
#include <iostream>
#include <algorithm>
using namespace std;
typedef std::pair<std::vector<unsigned>, std::vector<unsigned> > vec_pair;
bool sortingFunc(const pair<unsigned,vec_pair>& a, const pair<unsigned,vec_pair>& b)
{
    if((a.second).second.size() == (b.second).second.size()) {
        if(std::lexicographical_compare((a.second).second.begin(), (a.second).second.end(), (b.second).second.begin(), (b.second).second.end()))//a<b
        {
            return true;
        }else{
            if((a.second).first.size() == (b.second).first.size()) {
                return std::lexicographical_compare((a.second).first.begin(), (a.second).first.end(), (b.second).first.begin(), (b.second).first.end());
            } else {
                // Sort by size.
                return (a.second).first.size() < (b.second).first.size();
            }            
        }
    } else {
        // Sort by size.
        return (a.second).second.size() < (b.second).second.size();
    }
}
int main()
{
    vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp;
    std::sort(sbp.begin(), sbp.end(), sortingFunc);
}

我使用的是c++ 11 (gcc 4.8.2)

我将使用std::tiemake_tuple与右值:

bool sortingFunc(const pair<unsigned, vec_pair>& a, const pair<unsigned, vec_pair>& b)
{
    return std::make_tuple(a.second.second.size(), std::ref(a.second.second), a.second.first.size(), std::ref(a.second.first))
         < std::make_tuple(b.second.second.size(), std::ref(b.second.second), b.second.first.size(), std::ref(b.second.first));
}

if(std::lexicographical_compare((a.second).second.begin(), (a.second).second.end(), (b.second).second.begin(), (b.second).second.end()))//a<b
{
    return true;
}

没有b < a条件。

else if(std::lexicographical_compare((b.second).second.begin(), (b.second).second.end(), (a.second).second.begin(), (a.second).second.end()))//b < a
{
    return true;
}

代码的问题在于:

if(std::lexicographical_compare((a.second).second.begin(), (a.second).second.end(), (b.second).second.begin(), (b.second).second.end()))//a<b

您没有正确处理相反的条件。也就是说,上面这行只测试if (a.second.second < b.second.second)。如果它为真,则从函数返回真,这是正确的。但如果它为假,则继续检查较低优先级条件,忽略b.second.second可能小于a.second.second的可能情况。

同时,对Jarod42的std::tie方法做了一个小小的修改:

bool sortingFunc(const pair<unsigned, vec_pair>& a, const pair<unsigned, vec_pair>& b)
{
    auto  a1 = a.second.second.size();
    auto& a2 = a.second.second;
    auto  a3 = a.second.first.size();
    auto& a4 = a.second.first;
    auto  b1 = b.second.second.size();
    auto& b2 = b.second.second;
    auto  b3 = b.second.first.size();
    auto& b4 = b.second.first;
    return std::tie(a1, a2, a3, a4) < std::tie(b1, b2, b3, b4);
}

std::tie所做的是创建一个std::tuple引用到它的参数。operator<被重载,以便std::tuple对其元素从第一个到最后一个进行字典顺序比较。