C++ åèæå
- C++11
- C++14
- C++17
- C++20
- C++ ç¼è¯å¨æ¯ææ åµè¡¨
- ç¬ç«ä¸å®¿ä¸»å®ç°
- C++ è¯è¨
- C++ å ³é®è¯
- é¢å¤çå¨
- C++ æ ååºå¤´æä»¶
- å ·åè¦æ±
- åè½ç¹æ§æµè¯ (C++20)
- å·¥å ·åº
- ç±»åæ¯æï¼åºæ¬ç±»åãRTTIãç±»åç¹æ§ï¼
- æ¦å¿µåº (C++20)
- é误å¤ç
- 卿å å管ç
- æ¥æåæ¶é´å·¥å ·
- å符串åº
- 容å¨åº
- è¿ä»£å¨åº
- èå´åº (C++20)
- ç®æ³åº
- std::execution::sequenced_policy, std::execution::parallel_policy, std::execution::parallel_unsequenced_policy, std::execution::unsequenced_policy
- æå¶çº¦ç®æ³ (C++20 èµ·)
- std::is_execution_policy
- std::execution::seq, std::execution::par, std::execution::par_unseq, std::execution::unseq
- std::all_of, std::any_of, std::none_of
- std::for_each_n
- std::sort
- std::reduce
- std::inclusive_scan
- std::exclusive_scan
- std::random_shuffle, std::shuffle
- std::clamp
- std::equal
- std::is_permutation
- std::mismatch
- std::swap
- std::search
- std::transform
- std::for_each
- std::make_heap
- std::count, std::count_if
- std::adjacent_find
- std::find, std::find_if, std::find_if_not
- std::find_end
- std::find_first_of
- std::search_n
- std::lexicographical_compare
- std::lexicographical_compare_three_way
- std::copy, std::copy_if
- std::copy_n
- std::copy_backward
- std::move
- std::move_backward
- std::shift_left, std::shift_right
- std::fill
- std::fill_n
- std::generate
- std::generate_n
- std::iter_swap
- std::swap_ranges
- std::sample
- std::remove, std::remove_if
- std::replace, std::replace_if
- std::reverse
- std::rotate
- std::unique
- std::remove_copy, std::remove_copy_if
- std::replace_copy, std::replace_copy_if
- std::reverse_copy
- std::rotate_copy
- std::unique_copy
- std::is_partitioned
- std::partition_point
- std::partition
- std::partition_copy
- std::stable_partition
- std::is_sorted
- std::is_sorted_until
- std::stable_sort
- std::partial_sort
- std::partial_sort_copy
- std::nth_element
- std::lower_bound
- std::upper_bound
- std::binary_search
- std::equal_range
- std::merge
- std::inplace_merge
- std::set_difference
- std::set_intersection
- std::set_symmetric_difference
- std::set_union
- std::includes
- std::is_heap
- std::is_heap_until
- std::sort_heap
- std::push_heap
- std::pop_heap
- std::max
- std::max_element
- std::min
- std::min_element
- std::minmax
- std::minmax_element
- std::next_permutation
- std::prev_permutation
- std::iota
- std::inner_product
- std::adjacent_difference
- std::accumulate
- std::transform_reduce
- std::partial_sum
- std::transform_inclusive_scan
- std::transform_exclusive_scan
- std::qsort
- std::bsearch
- 注é
- æ°å¼åº
- è¾å ¥/è¾åºåº
- æä»¶ç³»ç»åº
- æ¬å°ååº
- æ£å表达å¼åº
- ååæä½åº
- çº¿ç¨æ¯æåº
- å®éªæ§ C++ ç¹æ§
- æç¨çèµæº
- ç´¢å¼
- std 符å·ç´¢å¼
- åç¨æ¯æ (C++20)
- C++ å ³é®è¯
ä½ç½®ï¼é¦é¡µ > C++ åèæå >ç®æ³åº > std::is_permutation
std::is_permutation
| å®ä¹äºå¤´æä»¶ <algorithm>
|
||
| (1) | ||
| template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++11 èµ·) (C++20 å) |
|
| template< class ForwardIt1, class ForwardIt2 > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20 èµ·) | |
| (2) | ||
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++11 èµ·) (C++20 å) |
|
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20 èµ·) | |
| (3) | ||
| template< class ForwardIt1, class ForwardIt2 > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++14 èµ·) (C++20 å) |
|
| template< class ForwardIt1, class ForwardIt2 > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20 èµ·) | |
| (4) | ||
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++14 èµ·) (C++20 å) |
|
| template< class ForwardIt1, class ForwardIt2, class BinaryPredicate > constexpr bool is_permutation( ForwardIt1 first1, ForwardIt1 last1, |
(C++20 èµ·) | |
è¥åå¨èå´ [first1, last1) ä¸å
ç´ çæåï¼ä½¿å¾è¯¥èå´çäº [first2,last2) ï¼åè¿å true ï¼è¥ä¸ç»åºï¼åå
¶ä¸ last2 代表 first2 + (last1 - first1) ã
p æ¯è¾å
ç´ ãè¥å®ä¸æ¯çä»·å
³ç³»åè¡ä¸ºæªå®ä¹ãåæ°
| first1, last1 | - | è¦æ¯è¾çå ç´ èå´ |
| first2, last2 | - | è¦æ¯è¾ç第äºèå´ |
| p | - | è¥å
ç´ åºè¢«å½åç¸çåè¿å âtrue çäºå
è°è¯ã è°è¯å½æ°çç¾ååºçä»·äºå¦ä¸ï¼  bool pred(const Type &a, const Type &b);
|
| ç±»åè¦æ± | ||
-ForwardIt1, ForwardIt2 å¿
须满足éçååè¿ä»£å¨ (LegacyForwardIterator) çè¦æ±ã
| ||
-ForwardIt1, ForwardIt2 å¿
é¡»æ¥æç¸åç value_type ã
| ||
è¿åå¼
è¥èå´ [first1, last1) æ¯ [first2, last2) çæåå为 true ã
å¤æåº¦
è³å¤åºç¨ O(N2) 次è°è¯ï¼æè¥åºåå·²ç»ç¸çï¼ååç¡®åºç¨ N 次ï¼å ¶ä¸ N=std::distance(first1, last1) ã
ç¶èï¼è¥ ForwardIt1 å ForwardIt2 满足éçéæºè®¿é®è¿ä»£å¨ (LegacyRandomAccessIterator) çè¦æ±ä¸ std::distance(first1, last1) != std::distance(first2, last2) ï¼åä¸åºç¨è°è¯ã
å¯è½çå®ç°
template<class ForwardIt1, class ForwardIt2> bool is_permutation(ForwardIt1 first, ForwardIt1 last, ForwardIt2 d_first) { // è·³è¿å ¬å ±åç¼ std::tie(first, d_first) = std::mismatch(first, last, d_first); // å¨ rest ä¸è¿ä»£ï¼è®¡æ° [d_first, d_last) ä¸åºç°å¤å°æ¬¡ // æ¯ä¸ªæ¥èª [first, 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; // å·²ç»éå°æ¤ *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
åé
| 产çæä¸ªå
ç´ èå´çæåå
¸é¡ºåºçä¸ä¸ä¸ªè¾å¤§çæå (彿°æ¨¡æ¿) | |
| 产çæä¸ªå
ç´ èå´çæåå
¸é¡ºåºçä¸ä¸ä¸ªè¾å°çæå (彿°æ¨¡æ¿) |