next_permutation
next_permutation()是按字典序依次排列的,当排列到最大的值是就会返回false.
生成给定序列的下一个排列,按字典序生成下一个排列。如果有下一个排列,返回true,否则返回false
template <typename _BidirectionalIterator, typename _Compare> bool __next_permutation(_BidirectionalIterator __first, _BidirectionalIterator __last, _Compare __comp) { if (__first == __last) return false; _BidirectionalIterator __i = __first; ++__i; if (__i == __last) return false; __i = __last; --__i;
for (;;) { _BidirectionalIterator __ii = __i; --__i; if (__comp(__i, __ii)) { _BidirectionalIterator __j = __last; while (!__comp(__i, --__j)) { } std::iter_swap(__i, __j); std::__reverse(__ii, __last, std::__iterator_category(__first)); return true; } if (__i == __first) { std::__reverse(__first, __last, std::__iterator_category(__first)); return false; } } }
|
参考
使用next_permutation()的坑,你中招了么?_辉小歌的博客-CSDN博客
C++神奇的next_permutation - 知乎 (zhihu.com)