std::is_permutation
Материал из cppreference.com
|
|
This page has been machine-translated from the English version of the wiki using Google Translate.
The translation may contain errors and awkward wording. Hover over text to see the original version. You can help to fix errors and improve the translation. For instructions click here. |
| Заголовочный файл <algorithm>
|
||
| template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first, ForwardIt1 last, |
(1) | (начиная с C++11) |
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first, ForwardIt1 last, |
(2) | (начиная с C++11) |
Возврат true, если существует перестановка элементов в диапазоне
[first1, last1), что делает этот диапазон равный диапазоне начиная с d_first. Первый вариант используется operator== за равноправие, вторая версия использует бинарный p предикатOriginal:
Returns true if there exists a permutation of the elements in the range
[first1, last1) that makes that range equal to the range beginning at d_first. The first version uses operator== for equality, the second version uses the binary predicate pThe text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
Содержание |
[править] Параметры
| first, last | - | диапазон элементов для сравнения
Original: the range of elements to compare The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| d_first | - | В начале второго диапазона для сравнения
Original: the beginning of the second range to compare The text has been machine-translated via Google Translate. You can help to correct and verify the translation. Click here for instructions. | |||||||||
| p | - | бинарный предикат, который возвращает true, если элементы следует считать равными. Сигнатура функции-предиката должны быть эквивалентна следующей:
В сигнатуре не обязательно наличие const &, но функция не должна изменять переданные ей объекты. | |||||||||
| Требования, накладываемые на типы | |||||||||||
-ForwardIt1, ForwardIt2 должен соответствовать требованиям ForwardIterator.
| |||||||||||
[править] Возвращаемое значение
true, если диапазон
[first, last) является перестановкой диапазоне начиная с d_first.Original:
true if the range
[first, last) is a permutation of the range beginning at d_first.The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Сложность
В большинстве O(N2) применения предиката, или точно N если последовательности уже равны, где N=std::distance(first, last).
Original:
At most O(N2) applications of the predicate, or exactly N if the sequences are already equal, where N=std::distance(first, last).
The text has been machine-translated via Google Translate.
You can help to correct and verify the translation. Click here for instructions.
You can help to correct and verify the translation. Click here for instructions.
[править] Возможная реализация
template<class ForwardIt1, class ForwardIt2> bool is_permutation(ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first) { // skip common prefix std::tie(first, d_first) = std::mismatch(first, last, d_first); // iterate over the rest, counting how many times each element // from [first, last) appears in [d_first, d_last) if (first != last) { ForwardIt2 d_last = d_first; std::advance(d_last, std::distance(first, last)); for (ForwardIt1 i = first; i != last; ++i) { if (i != std::find(first, i, *i)) continue; // already counted this *i auto m = std::count(d_first, d_last, *i); if (m==0 || std::count(i, last, *i) != m) { return false; } } } return true; } |
[править] Пример
#include <algorithm> #include <vector> #include <iostream> int main() { std::vector<int> v1{1,2,3,4,5}; std::vector<int> v2{3,5,4,1,2}; std::cout << "3,5,4,1,2 is a permutation of 1,2,3,4,5? " << std::boolalpha << std::is_permutation(v1.begin(), v1.end(), v2.begin()) << '\n'; std::vector<int> v3{3,5,4,1,1}; std::cout << "3,5,4,1,1 is a permutation of 1,2,3,4,5? " << std::boolalpha << std::is_permutation(v1.begin(), v1.end(), v3.begin()) << '\n'; }
Вывод:
3,5,4,1,2 is a permutation of 1,2,3,4,5? true 3,5,4,1,1 is a permutation of 1,2,3,4,5? false
[править] См. также
| generates the next greater lexicographic permutation of a range of elements (шаблон функции) | |
| generates the next smaller lexicographic permutation of a range of elements (шаблон функции) | |