next_premutation
来源:互联网 发布:淘宝旗袍模特最红 编辑:程序博客网 时间:2024/05/24 05:02
next_premutation
下午研究了一下全排列算法,然后发现C++的STL有一个函数可以方便地生成全排列,这就是next_permutation
在C++ Reference中查看了一下next_permutation的函数声明:
#include <algorithm>
bool next_permutation( iterator start, iterator end );
The next_permutation() functionattempts to transform the given range of elements [start,end) into the nextlexicographically greater permutation of elements. If it succeeds, it returnstrue, otherwise, it returns false.
从说明中可以看到 next_permutation的返回值是布尔类型。按照提示写了一个标准C++程序:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
#include <iostream>
#include <algorithm>
#include <string>
usingnamespace std;
int main()
{
string str;
cin>> str;
sort(str.begin(), str.end());
cout<< str<< endl;
while(next_permutation(str.begin(), str.end()))
{
cout<< str<< endl;
}
return0;
}
其中还用到了 sort函数和 string.begin()、string.end(),函数声明如下:
#include <algorithm>
void sort( iterator start, iterator end );
sort函数可以使用NlogN的复杂度对参数范围内的数据进行排序。
#include <string>
iterator begin();
const_iterator begin() const;
#include <string>
iterator end();
const_iterator end() const;
string.begin()和string.end()可以快速访问到字符串的首字符和尾字符。
在使用大数据测试的时候,发现标准C++的效率很差...换成C函数写一下,效率提升了不止一倍...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <cstdio>
#include <algorithm>
#include <cstring>
#define MAX 100
usingnamespace std;
int main()
{
int length;
char str[MAX];
gets(str);
length =strlen(str);
sort(str, str+ length);
puts(str);
while(next_permutation(str, str+ length))
{
puts(str);
}
return0;
}
- next_premutation
- next_premutation
- STL next_premutation函数的C++和C的作用。
- 快速学习stl(next_premutation)全排列;nyoj会排序的小明
- Hbase分析报告(tablename+startrowkey_regionid/-meta-)
- 集成声卡和独立声卡哪个的性能好些
- ABAP 获取 状态的两个函数 STATUS_TEXT_EDIT 和 STATUS_READ 的简单介绍
- Autotools
- HTML5的新特性的探讨
- next_premutation
- java(JNI)调用c/c++动态链接库 linux
- 利用systeminfo命令查看电脑系统安装时间等信息
- vector
- 17款响应式布局框架
- ios中利用协议实现回调函数功能
- 同问 集成网卡和独立网卡哪个好?
- JConsle的简单使用
- Fedora 日志