在c++ 中如何对数组中的元素进行全排列?

来源:百度知道 编辑:UC知道 时间:2024/09/23 09:31:48
如题
在c++中如何对某一个数组中的元素进行全排列
比如说对float型的数组进行全排列并输出
希望大虾们能给出算法

用标准库algorithm中的next_permutation, 或prev_permutation:

#include <iostream>
#include <algorithm>
#include <iterator>
using namespace std;

int main()
{
float fArray[] = {1.1, 2.2, 3.3, 4.4, 5.5};
float *Beg = fArray;
float *End = fArray + sizeof fArray / sizeof fArray[0];

do{
copy(Beg, End, ostream_iterator<float>(cout, " "));
cout << endl;
}while(next_permutation(Beg, End));
}

算法你去翻翻STL的源码不就可以了,而且这样的算法网上一搜遍地都是,这是我以前搜集的一个算法:

#include <iostream>
#include <algorithm>

template <class T>
void Print(T* tArray, int iSize)
{
for(int i = 0; i < iSize; ++i)
std::cout << tArray[i] << ' ';

std::cout << std::endl;
}

template <class T>
void Permutation(T* tArray, int iSize)
{
int* iTag = new int