目录

algorithm::is_permutation()

描述 (Description)

C ++函数std::algorithm::is_permutation()测试序列是否是其他序列的排列。 它使用operator ==进行比较。

声明 (Declaration)

以下是std :: algorithm :: is_permutation()函数形式std :: algorithm头的声明。

C++11

template <class ForwardIterator1, class ForwardIterator2>
bool is_permutation(ForwardIterator1 first1,ForwardIterator1 last1,
   ForwardIterator2 first2);

参数 (Parameters)

  • first1 - 将迭代器输入到第一个序列的初始位置。

  • last1 - 将迭代器输入到第一个序列的最终位置。

  • first2 - 将迭代器输入到第二个序列的初始位置。

返回值

如果第一个范围是另一个范围的排列,则返回true,否则返回false。

异常 (Exceptions)

如果元素比较或迭代器上的操作抛出异常,则抛出异常。

请注意,无效参数会导致未定义的行为。

时间复杂

二次。

例子 (Example)

以下示例显示了std :: algorithm :: is_permutation()函数的用法。

#include <iostream>
#include <vector>
#include <algorithm>
using namespace std;
int main(void) {
   vector<int> v1 = {1, 2, 3, 4, 5};
   vector<int> v2 = {5, 4, 3, 2, 1};
   bool result;
   result = is_permutation(v1.begin(), v1.end(), v2.begin());
   if (result == true)
      cout << "Both vector contains same elements." << endl;
   v2[0] = 10;
   result = is_permutation(v1.begin(), v1.end(), v2.begin());
   if (result == false)
      cout << "Both vector doesn't contain same elements." << endl;
   return 0;
}

让我们编译并运行上面的程序,这将产生以下结果 -

Both vector contains same elements.
Both vector doesn't contain same elements.
↑回到顶部↑
WIKI教程 @2018