STL is_permutation 的一个疑问

用户昵称13579 2014-09-20 09:26:07
引用
is_permutation:
http://www.cplusplus.com/reference/algorithm/is_permutation/?kw=is_permutation
Test whether range is permutation of another
Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match, even in a different order.
The elements are compared using operator== or pred.



int main()
{
cout << boolalpha;

vector<int> a = {1, 2, 3, 4, 5};
vector<int> b = {2, 3, 4, 0, 1};

cout << is_permutation(a.begin(), a.end(), b.begin(), greater<int>());
}


编译器为vs2013
以上输出结果为 false,按理来说应该是 true
经测试,如果使用 greater, less 等谓词均失败,但使用默认的 equal_to 后,输出符合我的想法,比如 {1, 2, 3} 和 { 2, 1, 3} 返回 true,{4, 5, 7} 和 {1, 6, 9} 返回 false。调用后第二个区间元素的排列顺序不变。

然后我自己写了一个 my_is_permutation,功能如下:
如果可通过重新排列第二个区间,使得第一个区间和第二个区间对应的元素都匹配,则返回true,否则返回false。调用后第二个区间内元素排列顺序会被打乱。若返回 true,两个区间符合要求。(为了方便测试,没有让第二个区间变回原样)。自己的代码就不贴了。
然后把上面代码中 is_permutation 换成 my_is_permutation 后得到 true。

又尝试了几组数据,调用 is_permutation 都不能达到效果(除非使用 equal_to),但 my_is_permutation 可以得到正确的结果

问题就是,对于一个这么简单的功能,库怎么可能出错。所以我怀疑是不是自己理解错了 is_permutation 的功能。请大家指教下。
...全文
162 5 打赏 收藏 转发到动态 举报
AI 作业
写回复
用AI写文章
5 条回复
切换为时间正序
请发表友善的回复…
发表回复
用户昵称13579 2014-09-27
  • 打赏
  • 举报
回复
引用 4 楼 FancyMouse 的回复:
[quote=引用 3 楼 Laster13579 的回复:] [quote=引用 2 楼 FancyMouse 的回复:] 标准规定要你传==,你传一个<进去,你说是谁的问题。
Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match, even in a different order. The elements are compared using operator== or pred. 那您用STL全部用默认参数好了,方便快捷[/quote] 这人什么理解能力。我从来就没说只能传operator==。我说的只是抽象意义的==。你传进去的pred也必须满足==的语义。啥叫==,等价关系要满足自反对称传递三个性质,要是不懂啥是等价关系自己补习离散代数基础。 你自己贴的那个链接,自己看pred的描述: The value returned indicates whether the elements are considered to match in the context of this function. 怪谁?[/quote] 不好意思,的确是我的问题,谢谢。 但是,我的确认为,您的==跟<表示都没带operator,我不认为我一开始把您的==理解成operator ==是有问题。而且恐怕您的回复语气从一开始就不太友善吧。 不过怎样,还是谢谢了!
FancyMouse 2014-09-22
  • 打赏
  • 举报
回复
引用 3 楼 Laster13579 的回复:
[quote=引用 2 楼 FancyMouse 的回复:] 标准规定要你传==,你传一个<进去,你说是谁的问题。
Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match, even in a different order. The elements are compared using operator== or pred. 那您用STL全部用默认参数好了,方便快捷[/quote] 这人什么理解能力。我从来就没说只能传operator==。我说的只是抽象意义的==。你传进去的pred也必须满足==的语义。啥叫==,等价关系要满足自反对称传递三个性质,要是不懂啥是等价关系自己补习离散代数基础。 你自己贴的那个链接,自己看pred的描述: The value returned indicates whether the elements are considered to match in the context of this function. 怪谁?
用户昵称13579 2014-09-21
  • 打赏
  • 举报
回复
引用 2 楼 FancyMouse 的回复:
标准规定要你传==,你传一个<进去,你说是谁的问题。
Compares the elements in the range [first1,last1) with those in the range beginning at first2, and returns true if all of the elements in both ranges match, even in a different order. The elements are compared using operator== or pred. 那您用STL全部用默认参数好了,方便快捷
FancyMouse 2014-09-21
  • 打赏
  • 举报
回复
标准规定要你传==,你传一个<进去,你说是谁的问题。
熊熊大叔 2014-09-20
  • 打赏
  • 举报
回复
VS的实现为了对算法进行优化,可能期望 a pred b == b pred a (这里pred语义上是匹配) 你用greater做pred不满足这个条件,导致结果不可预期

65,189

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧