Doesnt stl sort require a strict weak ordering to work?

﹥>﹥吖頭↗ 提交于 2019-12-10 17:56:05

问题


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 ?


回答1:


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.



来源:https://stackoverflow.com/questions/16824667/doesnt-stl-sort-require-a-strict-weak-ordering-to-work

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!