检查[_First,_Last)区间内的元素是否为_First2起始之区间元素的一个排列组合,以operator ==比较元素
检查[_First,_Last)区间内的元素是否为_First2起始之区间元素的一个排列组合,以_Pred(elem1, elem2)比较元素,应该在elem1等于elem2时返回true
所有iterator必须有相同的value type
复杂度:最糟糕情况下是二次
…
…
以下是使用方法:
template<typename T>
inline void PRINT_ELEMENTS(const T & coll, const string& optcstr = "")
{
cout << optcstr;
for (auto elem : coll)
{
cout << elem << ' ';
}
cout << endl;
}
bool bothEvenOrOdd(int elem1, int elem2)
{
return elem1 % 2 == elem2 % 2;
}
int main()
{
vector<int>a{1,1,2,3,4,5,6,7,8,9};
list<int>b{1,9,8,7,6,5,4,3,2,1};
deque<int>c{11,12,13,19,18,17,16,15,14,11};
PRINT_ELEMENTS(a, "a: ");
PRINT_ELEMENTS(b, "b: ");
PRINT_ELEMENTS(c, "c: ");
if(is_permutation(a.cbegin(), a.cend(),b.cbegin()))
{
cout << "a and b have equal elements" << endl;
}
else
{
cout << "a and b don't have equal elements" << endl;
}
if (is_permutation(a.cbegin(), a.cend(), c.cbegin(), bothEvenOrOdd))
{
cout << "numbers of even and odd elements match" << endl;
}
else
{
cout << "numbers of even and odd elements don't match" << endl;
}
}