最近在网上找到一个巧妙的运用STL生成的排列组合代码。
感觉很好,记之:
#include
<
iostream
>
#include < algorithm >
#include < iterator >
using namespace std;
int main()
{
int a[ 3 ] = { 1 , 2 , 3 };
do
{
copy(a, a + 3 , ostream_iterator < int > (cout, " " ));
cout << endl;
} while (next_permutation(a, a + 3 ));
return 0 ;
}
#include < algorithm >
#include < iterator >
using namespace std;
int main()
{
int a[ 3 ] = { 1 , 2 , 3 };
do
{
copy(a, a + 3 , ostream_iterator < int > (cout, " " ));
cout << endl;
} while (next_permutation(a, a + 3 ));
return 0 ;
}
输出结果:
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1