使用STL的next_permutation函数生成全排列(C++)
来源:互联网 发布:caffe 用训练好的模型 编辑:程序博客网 时间:2024/04/29 18:32
#include <iostream>#include <algorithm>#include <string> using namespace 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; } return 0;}
在C++ Reference中查看了一下next_permutation的函数声明:
#include <algorithm>
bool next_permutation( iterator start, iterator end );
The next_permutation() function attempts to transform the given range of elements [start,end) into the next lexicographically greater permutation of elements. If it succeeds, it returns true, otherwise, it returns false.
从说明中可以看到 next_permutation 的返回值是布尔类型。按照提示写了一个标准C++程序:
其中还用到了 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函数写一下,效率提升了不止一倍...
#include <cstdio>#include <algorithm>#include <cstring>#define MAX 100 using namespace 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); } return 0;}
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列(C++)
- 使用STL的next_permutation函数生成全排列
- C++STL中全排列函数next_permutation的使用
- C++STL中全排列函数next_permutation的使用
- C++STL中全排列函数next_permutation的使用
- POJ 1833:排列 [#使用STL的next_permutation函数生成全排列#]
- poj 1833 排列 [ 使用STL的next_permutation函数生成全排列 + STL中copy函数的用法完美解决 ]
- 全排列函数next_permutation在STL的使用
- 使用next_permutation函数生成全排列
- STL: <algorithm>中 next_permutation()生成全排列
- STL之生成全排列:next_permutation & prev_permutation
- ava.util.Date和java.sql.Date 一点区别
- 服务好“最后一公里”,高效CDN架构经验
- python使用thrift出现TTransportException: TSocket read 0 bytes
- 两种不同随机算法设计理念
- mysql分区功能详解
- 使用STL的next_permutation函数生成全排列(C++)
- 259高校毕业设计选题
- 关于 Java 对象序列化您不知道的 5 件事
- 【SCOI2007】修车
- Android NDK R9C 安装与配置
- c++重写卷积网络的前向计算过程,复现theano的测试结果
- mysql 开启慢查询
- 知识管理软件要用哪款呐
- kindeditor不自动过滤html