描述
C++ 函數std::algorithm::is_permutation()測試一個序列是否是其他序列的排列。它用運算符 ==用於比較。
聲明
以下是 std::algorithm::is_permutation() 函數形式 std::algorithm 頭文件的聲明。
C++11
template <class ForwardIterator1, class ForwardIterator2> bool is_permutation(ForwardIterator1 first1,ForwardIterator1 last1, ForwardIterator2 first2);
參數
first1− 將迭代器輸入到第一個序列的初始位置。
last1− 將迭代器輸入到第一個序列的最終位置。
first2− 將迭代器輸入到第二個序列的初始位置。
返回值
如果第一個範圍是另一個範圍的排列,則返回真,否則返回假。
異常
如果元素比較或迭代器上的操作拋出異常,則拋出異常。
請注意無效的參數會導致未定義的行為。
時間複雜度
二次方。
示例
下麵的例子展示了 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.
相關用法
- C++ Algorithm is_permutation()用法及代碼示例
- C++ Algorithm is_partitioned()用法及代碼示例
- C++ Algorithm is_sorted()用法及代碼示例
- C++ Algorithm is_heap()用法及代碼示例
- C++ Algorithm is_sorted_until()用法及代碼示例
- C++ Algorithm is_heap_until()用法及代碼示例
- C++ Algorithm inplace_merge()用法及代碼示例
- C++ Algorithm includes()用法及代碼示例
- C++ Algorithm iter_swap()用法及代碼示例
- C++ Algorithm copy()用法及代碼示例
- C++ Algorithm remove_if()用法及代碼示例
- C++ Algorithm remove()用法及代碼示例
- C++ Algorithm max_element()用法及代碼示例
- C++ Algorithm equal()用法及代碼示例
- C++ Algorithm set_union()用法及代碼示例
- C++ Algorithm next_permutation()用法及代碼示例
- C++ Algorithm upper_bound()用法及代碼示例
- C++ Algorithm minmax()用法及代碼示例
- C++ Algorithm remove_copy_if()用法及代碼示例
- C++ Algorithm find_if_not()用法及代碼示例
注:本文由純淨天空篩選整理自 C++ Algorithm Library - is_permutation() Function。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。