next_permutation 的使用
来源:互联网 发布:怎么能提高淘宝销量 编辑:程序博客网 时间:2024/05/18 03:07
重排区间中的元素,使得原序列变成按字典次序的下一个序列。其中的“下一个”的定义可以由用户自己定制。
加载头文件:
#include<iostream>
函数原型:
template<class BidirectionalIterator>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last
);
template<class BidirectionalIterator, class BinaryPredicate>
bool next_permutation(
BidirectionalIterator _First,
BidirectionalIterator _Last,
BinaryPredicate _Comp
);
传入参数说明:
_First 要改变的第一个序列的起始位置
_Last 要改变的第一个序列的结束位置
_Comp 自定义的大小关系函数对象,根据两元素的大小返回true或者false
函数返回值:
若字典次序的下一个序列存在且已经替换排序成功则返回true,否则返回false,同时排序方案被转换成按字典次序最小的排序。
对于第二个重载函数的第三个参数,默认比较顺序为小于。如果找到下一个序列,则返回真,否则返回假。
说明:
默认的大小关系由小于运算符定义,区间中的元素必须是能够进行大小判断的。该算法的时间复杂性事线性的,最多进行(_Last-_First)/2次交换。
下面给出例子学习next_permutation的使用:序列{a, d, c, e, b}的下一个序列是什么呢?请利用前面的分析推出答案,并用代码验证。
我这里分别用数组和vector来表示序列,用next_permutation得到下一个序列.
#include <iostream>#include <algorithm>#include <vector>using namespace std;void TestArray(){char chs[] = { 'a', 'd', 'c', 'e', 'b' };int count = sizeof(chs) / sizeof(char);next_permutation(chs, chs + count);cout << "TestArray:" << endl;for (int i = 0; i < count; i++)cout << chs[i] << " ";cout << endl;}void TestVector(){char chs[] = { 'a', 'd', 'c', 'e', 'b' };int count = sizeof(chs) / sizeof(char);vector<char> vChs(chs, chs + count);next_permutation(vChs.begin(), vChs.end());cout << "TestArray:" << endl;vector<char>::iterator itr;for (itr = vChs.begin(); itr != vChs.end(); itr++) cout << *itr << " ";cout << endl;}int main(){TestArray();cout << endl;TestVector();cout << endl;return 0;}
- next_permutation 的使用
- next_permutation的使用
- 使用stl的next_permutation
- next_permutation的使用
- next_permutation函数的使用
- next_permutation 的使用
- next_permutation的原理和使用
- c++ next_permutation()函数的使用
- POJ1146初学使用偷懒的next_permutation()
- 【STL】next_permutation的原理和使用
- 排列函数 prev_permutation和next_permutation的使用
- hdu1716 STL next_permutation函数的使用
- 【转】【STL】next_permutation的原理和使用
- 【STL】next_permutation的原理和使用
- STL算法:prev_permutation和next_permutation的使用
- 【STL】next_permutation的原理和使用
- STL算法:prev_permutation和next_permutation的使用
- hdu 1027 hdu 1716 (next_permutation的使用)
- Description Resource Path LocationType Java compiler level does not match the version of the instal
- WINDOWS下VIM配置
- 通过函数调用来实现冒泡排序法
- java.lang.LinkageError: JAXB 2.0 API is being loaded from the bootstrap classloader, but this RI ***
- 欢迎使用CSDN-markdown编辑器
- next_permutation 的使用
- shell 生成随机密码
- linq to entity 的文集集锦
- 路由跳数
- fgets读取一行数据
- C#中 一个多线程框架
- C#异步跨线程
- 1002455 - Errors in texts: Messages VX 201, VX 111, and VX 205
- Android版busybox编译