繁体   English   中英

在C ++中对向量进行排序

[英]Sorting vectors in c++

我需要先按sbp.second.second向量vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp数据结构vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp >>>> vector<pair<unsigned, pair<vector<unsigned>, vector<unsigned> > > > sbp进行排序,并以sbp.second等于sbp.second.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与rvalue:

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) 如果为true,则从函数返回true,这是正确的。 但是,如果它为假,则继续检查较低优先级的条件,而忽略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重载, std::tuple对其元素进行字典编排比较。

暂无
暂无

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

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