<myclass> 使用 STL 中的排序函数在一行中对 std::vector 进行排序

Sort std::vector<myclass> in one line using sort function from STL

本文关键字:排序 一行 vector std 函数 gt myclass lt 使用 STL      更新时间:2023-10-16

问题是关于使用函数sort从STL的算法类排序std::vector<myclass>
标准方式为:sort(v.begin(), v.end(), &myfunct)
其中myfunct为:

bool myfunct( myclass first, myclass second ) {   
    if (first.value < second.value)   
        return true;   
    else return false;   
}
上面的

方法需要多行。我很好奇如何在一行中完成它。是否有可能定义函数,比较我的类对象内排序函数?可能是用了这个(a < b) ? a : b。我记得c#中有类似的东西,但我忘了它是怎么叫的。

首先,您可以只返回first.value < second.value,但这并不能消除该函数。在c++ 2011中,你可以使用lambda函数:

   std::sort(begin, end, [](myclass const& f, myclass const& s){ return f.value < s.value; });

如果没有c++ 2011,我认为你需要一个函数对象,因为没有任何东西可以将你的类投影到你实际想要比较的值。

顺便说一句,除了最简单的对象之外,你肯定想把所有的对象都传递给比较函数。

您可以使用boost::lambdaboost::lambda::bind(带有boost lambda占位符)

std::sort(vec.begin(), vec.end(),
            boost::lambda::bind(&A::a, boost::lambda::_1)
            <
            boost::lambda::bind(&A::a, boost::lambda::_2));

sort将2个值传递给比较函数,因此您需要比较这2个值。代码的bind部分只是从struct A(由_1_2引用)中选择变量a

示例代码:

#include <iostream>
#include <algorithm>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <boost/array.hpp>
struct A
{
    A() : a(0), b(0) {}
    int a;
    int b;
};
std::ostream & operator<<(std::ostream & os, A & a)
{ return os << a.a << ":" << a.b; }

int main()
{
    boost::array<A,5> vec;
    std::fill(vec.begin(),vec.end(),A());
    vec[0].a = 1;
    vec[1].a = 3;
    vec[2].a = 4;
    vec[3].a = 0;
    vec[4].a = 2;
    std::for_each(vec.begin(),vec.end(), std::cout << boost::lambda::_1 << ' ');
    std::cout << std::endl;
    std::sort(vec.begin(), vec.end(),
            boost::lambda::bind(&A::a, boost::lambda::_1)
            <
            boost::lambda::bind(&A::a, boost::lambda::_2));
    std::for_each(vec.begin(),vec.end(), std::cout << boost::lambda::_1 << ' ');
    std::cout << std::endl;
}
输出:

1:0 3:0 4:0 0:0 2:0 
0:0 1:0 2:0 3:0 4:0

为什么不将vector复制到set中:

std::copy(v.begin(),v.end(),std::inserter(s,s.end()));

现在集合中的元素按升序排序,现在使用set

对sort()的一行调用:sort(my_vector_of_class_object.begin(),my_vector_of_class_object.end(),compare);

"类对象的排序向量"的工作演示代码如下:

#include <iostream>
#include <vector>
#include <string>
#include <algorithm>
using namespace std;
class my_Class
{
    public:
    my_Class(int r,int n, int s):rollno(r),name(n),status(s) { }
    int getRollno() const { return rollno;}
    int getName() const { return name;}
    int getStatus() const { return status;}
    private:
    int rollno;
    int name;
    int status;
    };
    bool compare(const my_Class& x, const my_Class& y) {
    return x.getRollno() < y.getRollno();
}
int main()
{
    vector<my_Class> my_vector_of_class_object;
    vector<my_Class>::const_iterator iter;
    my_Class s1(10,20,30);
    my_Class s2(40,50,60);
    my_Class s3(25,85,9);
    my_Class s4(1,50,2);
    my_Class s5(90,70,90);
    my_Class s6(85,85,3);
    my_Class s7(20,6,89);
    my_Class s8(70,54,22);
    my_Class s9(65,22,77);

    my_vector_of_class_object.push_back(s1);
    my_vector_of_class_object.push_back(s2);
    my_vector_of_class_object.push_back(s3);
    my_vector_of_class_object.push_back(s4);
    my_vector_of_class_object.push_back(s5);
    my_vector_of_class_object.push_back(s6);

    my_vector_of_class_object.push_back(s7);
    my_vector_of_class_object.push_back(s8);
    my_vector_of_class_object.push_back(s9);

    cout <<"Before vector sort n";
    for(iter=my_vector_of_class_object.begin(); iter!=my_vector_of_class_object.end();++iter)
    std::cout << (*iter).getRollno() << 't' << (*iter).getName() << 't' << (*iter).getStatus() << 'n';
    cout <<"  nn";
    sort(my_vector_of_class_object.begin(),my_vector_of_class_object.end(),compare);

    cout <<"After vector sort n";
    for(iter=my_vector_of_class_object.begin(); iter!=my_vector_of_class_object.end();++iter)
    std::cout << (*iter).getRollno() << 't' << (*iter).getName() << 't' << (*iter).getStatus() << 'n';
    cout <<"  nn";

return 0;
}