类别:算法 | 组件类型:函数 |
template <class BidirectionalIterator> bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last); template <class BidirectionalIterator, class StrictWeakOrdering> bool prev_permutation(BidirectionalIterator first, BidirectionalIterator last, StrictWeakOrdering comp);
后置条件是元素的新排列在字典序中比旧排列小(由lexicographical_compare决定),当且仅当返回值为true.
这两个版本的prev_permutation在定义一个元素是否小于另一个元素时有所不同。第一个版本使用operator<比较对象,第二个版本使用 函数对象comp.
int main() { int A[] = {2, 3, 4, 5, 6, 1}; const int N = sizeof(A) / sizeof(int); cout << "Initially: "; copy(A, A+N, ostream_iterator<int>(cout, " ")); cout << endl; prev_permutation(A, A+N); cout << "After prev_permutation: "; copy(A, A+N, ostream_iterator<int>(cout, " ")); cout << endl; next_permutation(A, A+N); cout << "After next_permutation: "; copy(A, A+N, ostream_iterator<int>(cout, " ")); cout << endl; }
备注[first, last)[1] 如果N!中的所有元素彼此不同,那么恰好有3!/2!个排列。不过,如果某些元素与其他元素相同,那么排列会更少。例如,元素1 1 2.
只有三个(