写的一个排列组合算法,用了函数模板,经测试没有发现问题,
#include<iostream>
#include<cstdlib>
#include<vector>
#include<string>
#include<algorithm>
#include<iterator>
using namespace std;
//对单个元素的输出方式
template<class T>
class Print_Elem
{
public:
void operator()(const T&elem)
{
cout<<elem<<" ";
}
};
//先对vec_elem对行排序,就可以最后以从小到大的全排列输出来了!!
template<class T>
void arrarge(vector<T>&vec_out,const vector<T>&vec_elem)
{
if(vec_out.size()==vec_elem.size())
{
for_each(vec_out.begin(),vec_out.end(),Print_Elem<T>());
cout<<endl;
}
else
{
for(int i=0;i<vec_elem.size();i++)
{
if(find(vec_out.begin(),vec_out.end(),vec_elem[i])==vec_out.end())
{
vec_out.push_back(vec_elem[i]);
arrarge(vec_out,vec_elem);
vec_out.pop_back();
}
}
}
}
int main()
{
int array[]={1,2,3};
char arr[]={'a','b','c','e','f'};
string arr_str[]={"i love you","c language is very interesting","china"};
vector<int>vec_out;
vector<int>vec_elem(array,array+sizeof(array)/sizeof(array[0]));
vector<char>vec_out_char;
vector<char>vec_elem_char(arr,arr+sizeof(arr)/sizeof(arr[0]));
vector<string>vec_str;
vector<string>vec_src(arr_str,arr_str+sizeof(arr_str)/sizeof(arr_str[0]));
vec_out.clear(),vec_out_char.clear();
arrarge(vec_out,vec_elem);
arrarge(vec_out_char,vec_elem_char);
arrarge(vec_str,vec_src);
system("PAUSE");
return 0;
}