Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
526 views
in Technique[技术] by (71.8m points)

c++ - Doesnt stl sort require a strict weak ordering to work?

From http://stdcxx.apache.org/doc/stdlibref/less-equal.html

--

You can pass a less_equal object to any algorithm that requires a binary function. For example, the sort() algorithm can accept a binary function as an alternate comparison object to sort a sequence. less_equal would be used in that algorithm in the following manner:

vector<int> vec1;
sort(vec1.begin(), vec1.end(),less_equal<int>());

--

Now I am confused, is the documentation above correct ?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

You are right, std::sort requires the comparer to define a strict weak ordering.

Which means that std::less_equal should not be used with std::sort. It can still be used with a number of other standard algorithms though, which take a binary function and which do not have the strict weak ordering requirement.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...