How to sort an STL vector?

前端 未结 5 2289
借酒劲吻你
借酒劲吻你 2020-12-04 10:44

I would like to sort a vector

vector object;

Where myclass contains many int variabl

5条回答
  •  旧时难觅i
    2020-12-04 11:05

    Like explained in other answers you need to provide a comparison function. If you would like to keep the definition of that function close to the sort call (e.g. if it only makes sense for this sort) you can define it right there with boost::lambda. Use boost::lambda::bind to call the member function.

    To e.g. sort by member variable or function data1:

    #include 
    #include 
    #include 
    #include 
    using boost::lambda::bind;
    using boost::lambda::_1;
    using boost::lambda::_2;
    
    std::vector object(10000);
    std::sort(object.begin(), object.end(),
        bind(&myclass::data1, _1) < bind(&myclass::data1, _2));
    

提交回复
热议问题