C++常用库函数
next_permutation函数
排列就是一次对对象序列或值序列的重新排列。例如,“ABC”中字符可能的排列是:
"ABC", "ACB", "BAC", "BCA", "CAB", "CBA"
头文件:
next_permutation()在使用前需要对欲排列数组按升序排序,顺序从小到大
当当前序列不存在下一个排列时,函数返回false,否则返回true
还有个跟它相反的函数:prev_permutation
功能:输出所有比当前排列小的排列,顺序从大到小
int类型
用法格式:
int a[3] = {1,2,3};
do
{
cout << a[0] << " " << a[1] << " " << a[2] << endl;
} while (next_permutation(a, a + 3)); //参数a代表要排列的开始位置;3指的是要进行排列的长度(结束位置)
char类型
char arr[20];
cin >> arr;
char* frist =arr;
char* last = arr+strlen(arr);
sort(frist, last);//该语句对输入的数组进行字典升序排序
do
{
cout << arr << endl;
} while (next_permutation(frist,last));
//这样就不必事先知道arr的大小了,是把整个arr字符串全都进行排序
//若采用 while(next_permutation(arr,arr+5)); 如果只输入1562,就会产生错误,因为arr中第五个元素指向未知
//若要整个字符串进行排序,参数5指的是数组的长度,不含结束符
string类型
string arr = "7564";
sort(arr.begin(), arr.end());
do
{
cout << arr << " ";
} while (next_permutation(arr.begin(), arr.end()));



