转自http://blog.sina.com.cn/s/blog_6635898a0102e0k9.html
在C++的标准函数库STL中,next_permutation()函数用于求数列的全排列。
函数原型:
template<class BidirectionalIterator>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
template<class BidirectionalIterator, class BinaryPredicate>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
);
两个重载函数,第二个带谓词参数_Comp,其中只带两个参数的版本,默认谓词函数为"小于"。
返回值:bool类型(当前数列是全排列最后一个时,返回false)
例子1(int型):
int main(){
int a[] = {3,1,2};
do{
cout << a[0] << " " << a[1] << " " << a[2] << endl;
}while (next_permutation(a, a+3));
return 0;
}
输出:312/321 因为原数列不是从最小字典排列开始。
所以要想得到所有全排列
int a[] = {3,1,2}; 然后先排序为:a[] = {1,2,3};
例子2(string型)
int main(){
string str;
cin >> str;
sort(str.begin(), str.end());
do{
cout << str << endl;
}while (next_permutation(str.begin(), str.end()));
return 0;
}
库中另一函数prev_permutation()与next_permutation()相反,懂的...
相关推荐
一:next_permutation(start,end,//cmp) 使用默认排序方法:按照字典序从小到大 int arr[3]={1,2,3}; do{ for(int num:arr){ cout<<num<< ; } cout<<endl; }while(next_permutation...
STL介绍 3 1、STL简介 3 2、算法 3 3、容器 3 4、迭代器 4 5、使用注意 4 ...18. next_permutation / prev_permutation 36 19. power 37 20. heap operations 38 21. min / max / swap 39 22. numeric_limits 39
常见得全排列有三种解决方案,for循环穷举,stl摸板函数next_permutation,还有DFS深度优先搜索,当我们遇到带有重复的字符串时应该考虑除去重复的部分。
23.28 下一排列组合next_permutation 406 23.29 上一排列组合prev_permutation 409 23.30 本章小结 411 第24章 数值算法 412 24.1 递增赋值iota 412 24.2 元素求和accumulate 413 24.3 两序列元素内积...
1.9.1 stl_config.h 中的各种组态 027 组态3:static template member 027 组态5:class template partial specialization 028 组态6:function template partial order 028 组态7:explicit function template ...
我只是将 C++ STL 函数 next_permutation 包装在 Mex 中。 若要使用,请先使用“ mex nextperms.cpp”为您的系统编译。 有关文档,请参阅 nextperms.m 和 nextperms_example_script.m。 典型的用例是您需要遍历...
23.28 下一排列组合next_permutation 406 23.29 上一排列组合prev_permutation 409 23.30 本章小结 411 第24章 数值算法 412 24.1 递增赋值iota 412 24.2 元素求和accumulate 413 24.3 两序列元素内积...
6.7.5 next_permutation 380 6.7.6 prev_permutation 382 6.7.7 random_shuffle 383 6.7.8 partial_sort / partial_sort_copy 386 6.7.9 sort 389 6.7.10 equal_range(应用于有序区间) 400 6.7.11 inplace_...
23.28 下一排列组合next_permutation 406 23.29 上一排列组合prev_permutation 409 23.30 本章小结 411 第24章 数值算法 412 24.1 递增赋值iota 412 24.2 元素求和accumulate 413 24.3 两序列元素内积...
STL算法常用函数:分隔组合:next_permutation()是检索当前范围内的划分,并重新排序为下一个替换,leetcode 556下一个元素元素III prev_permutation()是替换指定范围内的序列重新排列它重新排序为上一个序列。...
34.28 next_permutation 34.29 nth_element 34.30 partial sort 34.31 partial sort_copy 34.32 partition 34.33 pop_heap 34.34 prev_permutation 34.35 push_heap 34.36 random_shuffle 34.37 remove,remove_if,...
1.1当析构函数遇到多线程. . . . . . . . . . . . . . . . .. . . . . . . . . . . 3 1.1.1线程安全的定义. . . . . . . . . . . . . . . . .. . . . . . . . . . . 4 1.1.2MutexLock 与MutexLockGuard. . . . . . ....