Why does this use of std::sort with a custom comparator not compile?

前端 未结 1 1204
余生分开走
余生分开走 2021-01-21 12:48

I trying to use sort algorithm for sorting the elements of a vector. This is my code snippet.

Comparator

struct comparator
 {
    boo         


        
相关标签:
1条回答
  • 2021-01-21 13:31

    The comparator functor is supposed to take the elements but not the iterators as the parameter for comparing.

    You should change the parameter type of comparator::operator() from iterator to value type:

    struct comparator
    {
        bool operator() ( const pair<int, pair<CgpPop*,CgpPop*> > & lhs, const pair<int, pair<CgpPop*,CgpPop*> > & rhs) const
        {
            return lhs.first < rhs.first;
        }
    };
    

    BTW: Making operator() const member function is a good habit.

    0 讨论(0)
提交回复
热议问题