STL next_permutation和prev_permutation函数
利用next_permutation实现全排列升序输出,从尾到头找到第一个可以交换的位置,
直接求到第一个不按升序排列的序列。
#include <iostream>
#include <algorithm> /// next_permutation, sort
#define MAX 100
using namespace std; int main() {
int myints[MAX],n;
cin >> n;
for (int i = ; i < n; i++)
{
cin >> myints[i];
}
sort(myints, myints + n); do {
for (int i = ; i < n; i++)
{
cout << myints[i] <<" ";
}
cout << endl;
} while (next_permutation(myints, myints + n)); ///获取下一个较大字典序排列 system("pause");
return ;
}

同理,prev_permutation恰恰相反。
附一个全排列字母输出的例子:
题目描述
输入描述:
输入一个字符串,长度不超过9(可能有字符重复),字符只包括大小写字母。
#include "iostream"
#include "string"
#include "vector"
#include "algorithm" using namespace std; void Permutation(string str) {
vector<char> sstr;
for (int i = ; i < str.length(); i++)
sstr.push_back(str[i]); do {
for (int i = ; i < str.length(); i++)
{
cout << sstr[i] << " ";
}
cout << endl; } while (next_permutation(sstr.begin(), sstr.end())); } int main() {
string str;
while (cin >> str)
{
Permutation(str);
}
return ;
}
STL next_permutation和prev_permutation函数的更多相关文章
- STL中关于全排列next_permutation以及prev_permutation的用法
这两个函数都包含在algorithm库中.STL提供了两个用来计算排列组合关系的算法,分别是next_permutation和prev_permutation. 一.函数原型 首先我们来看看这两个函数 ...
- C++全排列函数next_permutation()和prev_permutation()
头文件:#include<algorithm> * * * 1. next_permutation(): next_permutation()函数的返回类型是bool类型. 即:如果有一个 ...
- C++ STL, next_permutation用法。
next_permutation 将按字母表顺序生成给定序列的下一个较大的序列,直到整个序列为 #include"iostream" #include"algorithm ...
- 打印全排列和stl::next_permutation
打印全排列是个有点挑战的编程问题.STL提供了stl::next_permutation完美的攻克了这个问题. 可是,假设不看stl::next_permutation,尝试自己解决,怎么做? 非常自 ...
- c++ stl容器set成员函数介绍及set集合插入,遍历等用法举例
c++ stl集合set介绍 c++ stl集合(Set)是一种包含已排序对象的关联容器.set/multiset会根据待定的排序准则,自动将元素排序.两者不同在于前者不允许元素重复,而后者允许. 1 ...
- 学习hash_map从而了解如何写stl里面的hash函数和equal或者compare函数
---恢复内容开始--- 看到同事用unordered_map了所以找个帖子学习学习 http://blog.sina.com.cn/s/blog_4c98b9600100audq.html (一)为 ...
- C++STL中的unique函数解析
一.总述 unique函数属于STL中比较常用函数,它的功能是元素去重.即”删除”序列中所有相邻的重复元素(只保留一个).此处的删除,并不是真的删除,而是指重复元素的位置被不重复的元素给占领了(详细情 ...
- STL之vector常用函数笔记
STL之vector常用函数笔记 学会一些常用的vector就足够去刷acm的题了 ps:for(auto x:b) cout<<x<<" ";是基于范围的 ...
- 「STL中的常用函数 容器」
占个坑,下午在更 二分操作:lower_bound和upper_bound 存图/数列操作:vector容器 全排列:next_permutation和prev_permutation 字符串转数列: ...
随机推荐
- SSH原理与运用(一):远程登录
SSH是每一台Linux电脑的标准配置. 随着Linux设备从电脑逐渐扩展到手机.外设和家用电器,SSH的使用范围也越来越广.不仅程序员离不开它,很多普通用户也每天使用. SSH具备多种功能,可以用于 ...
- java实现Haffman编码
1.先创建一个树节点类(泛型类),为了方便使用集合的排序方法,泛型类要实现泛型接口Comparable,代码如下 package com.hjp.huffman; /** * Created by J ...
- 《深入理解bootstrap》读书笔记:第4章 CSS组件(上)
bootstrap三大核心之二. 包括glyphicon图标,下拉菜单(dropdown),按钮组(button group)....等等. 一. 图标字体 bootstrap3提供了200多个免费图 ...
- ast模块
有这么一个需求,你想从文件中读取字典,方法有很多,这里用的是ast模块 import ast with open("account","r",encoding= ...
- $.ajax()引发的对Deferred的总结 (转)
传统的ajax写法: $.ajax({ url:"1.json", type:"get", success:function(data){}, error:fu ...
- thikphp创建共享数据config.php
要求:前台,后台:只需要配置一个config.php 其他文件共享 默认配置是 Index/Conf/config.php Admin/Conf/config.php 代码: return array ...
- 居于Web的进度条实现思路(下载百分比)
http://www.cnblogs.com/wfyfngu/p/4866434.html 在传统桌面项目中,进度条随处可见,但作为一个很好的用户体验,却没有在如今主流的B/S程序中得到传承,不能不说 ...
- 两种html幻灯片效果
650) this.width=650;" src="http://img1.51cto.com/attachment/201307/165757318.jpg" tit ...
- Mongodb副本集搭建经验
一.环境配置经验 1.一般安装的副本集的时候,主实例可以有数据库和用户:从实例不能.仲裁机不能有任何数据库包括用户 2.搭建副本集的时候Host使用外网IP,否则使用Mongodb VUE 1.6.9 ...
- C语言动态内存分配
考虑下面三段代码: 片段1 void GetMemory(char *p) { p = (); } void Test(void) { char *str = NULL; GetMemory(str) ...