qut*_*tab 7 c++ sorting stl stl-algorithm
我有一个类指针向量std::vector<Square*> listSquares.我想用类的一个属性作为关键字对它进行排序.这就是我正在做的事情
bool compById(Square* a, Square* b)
{
return a->getId() < b->getId();
}
std::sort(listSquares.begin(), listSquares.end(), compById)
Run Code Online (Sandbox Code Playgroud)
但编译器说:错误:没有匹配函数调用'sort(std :: vector :: iterator,std :: vector :: iterator,<unresolved overloaded function type>)'
我在这做错了什么?
joh*_*ohn 12
为了使用它compById作为参数,std::sort它不应该是一个成员函数.这是错的
class Square
{
bool compById(Square* a, Square* b)
{
return a->getId() < b->getId();
}
...
};
Run Code Online (Sandbox Code Playgroud)
这个更好,
class Square
{
...
};
bool compById(Square* a, Square* b)
{
return a->getId() < b->getId();
}
Run Code Online (Sandbox Code Playgroud)