c++ sorting with custom compare function

前端 未结 2 1290
一生所求
一生所求 2021-01-23 13:51

I have a vector of following type:

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

I

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-23 14:37

    Lambdas:

    std::sort(
        neighbors.begin(),
        neighbors.end(),
        [index](const std::pair< std::pair< int, int >, std::vector > &a,  
                const std::pair< std::pair< int, int >, std::vector > &b)
                { 
                     return ( a.second[index] > b.second[index] );
                }
    );  
    

    See What is a lambda expression in C++11? for a detailled introduction.

提交回复
热议问题