Can anybody explain this behavior in context of STL sort
algorithm?
If operator <
is not defined const
it gives error,
error: passing ‘const B’ as ‘this’ argument of ‘bool B::operator<(const B&)’ discards qualifiers [-fpermissive]
while (__pivot < *__last)
Is sort
algo lhs const
object or sort
is const
method?
class B
{
public:
...
bool operator < (const B& b) const // why const required here?
{
return (m_i < b.m_i);
}
...
private:
int m_i;
int m_j;
};
int main()
{
vector<B> Bvec2 {B(5), B(3), B(30), B(20), B(8)};
std::sort(Bvec2.begin(), Bvec2.end());
...
}
See Question&Answers more detail:os