检测两个区间,在不考虑数序的的情况下的相等性。
bool is_permutation (ForwardIterator1 beg1, ForwardIterator1 end1,ForwardIterator2 beg2);
bool is_permutation (ForwardIterator1 beg1, ForwardIterator1 end1,ForwardIterator2 beg2,CompFunc op);
- 两种情况都返回在不考虑顺序的情况下,第一个区间是否与第二个区间相等,第一种情况使用“==”比较,第二种情况使用返回bool的二元谓语op比较。
- op不应该在函数运行过程中改变参数的值,也不应在函数运行过程中改变状态。
- 所有的迭代器都应指向相同的类型。
- 这里的op必须是对称、自反且可传递的。
例:
#include <vector>
#include <list>
#include <algorithm>
#include <iostream>
#include <iterator>
#include <functional>
using namespace std;
bool fun(int n,int m)
{
return (n == m * 2);
}
bool bothEvenOrOdd(int elem1, int elem2)
{
return elem1 % 2 == elem2 % 2;
}
int main()
{
vector<int>v{ 1,2,3,4 };
list<int>lst{ 4,3,1,2,1};
list<int>lst1{4,2,6,8};
cout << is_permutation(v.begin(), v.end(), lst.begin())<<ends;
cout << is_permutation(v.begin(), v.end(), lst1.begin(),fun) << endl;
vector<int> coll1;
list<int> coll2;
deque<int> coll3;
coll1 = { 1, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
coll2 = { 1, 9, 8, 7, 6, 5, 4, 3, 2, 1 };
coll3 = { 12, 11, 13, 19, 18, 17, 16, 15, 14, 11 };
if (is_permutation(coll1.cbegin(), coll1.cend(), coll2.cbegin()))
{
cout << "coll1 and coll2 have equal elements" << endl;
}
else
{
cout << "coll1 and coll2 don’t have equal elements" << endl;
}
if (is_permutation(coll1.cbegin(), coll1.cend(), coll3.cbegin(), bothEvenOrOdd))
{
cout << "numbers of even and odd elements match" << endl;
}
else
{
cout << "numbers of even and odd elements don’t match" << endl;
}
return 0;
}
输出结果:1 0
coll1 and coll2 have equal elements
numbers of even and odd elements match