std::is_permutation
Da cppreference.com.
|
|
Questa pagina è stata tradotta in modo automatico dalla versione in ineglese della wiki usando Google Translate.
La traduzione potrebbe contenere errori e termini strani. Muovi il puntatore sopra al testo per vedere la versione originale. Puoi aiutarci a correggere gli gli errori. Per ulteriori istruzioni clicca qui. |
<metanoindex/>
<tbody> </tbody>| Elemento definito nell'header <algorithm>
|
||
template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first ); |
(1) | (dal C++11) |
template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first, BinaryPredicate p ); |
(2) | (dal C++11) |
Ritorna
true se esiste una permutazione degli elementi della [first1, last1) gamma che rende tale intervallo uguale all'inizio gamma a d_first. La prima versione utilizza operator== per l'uguaglianza, la seconda versione utilizza il p binario predicatoOriginal:
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.
Parametri
| first, last | - | la gamma di elementi da confrontare
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 | - | all'inizio del secondo intervallo da confrontare
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 | - | binary predicate which returns true if the elements should be treated as equal. The signature of the predicate function should be equivalent to the following:
The signature does not need to have |
| Type requirements | ||
-ForwardIt1, ForwardIt2 must meet the requirements of ForwardIterator.
| ||
Valore di ritorno
true se il [first, last) intervallo è una permutazione di inizio gamma a 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.
Complessità
Alla maggior parte delle applicazioni O(N2) del predicato, o esattamente N se le sequenze sono già uguali,
N=std::distance(first, last) dove.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.
Possibile implementazione
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;
}
|
Esempio
#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';
}
Output:
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
Vedi anche
| generates the next greater lexicographic permutation of a range of elements (funzione di modello) | |
| generates the next smaller lexicographic permutation of a range of elements (funzione di modello) | |