STLで順列
C++で順列を得る方法をちょっとメモ。こんな感じで。
#include
<vector>#include
<list>#include
<set>#include
<string>#include
<algorithm>#include
<iostream>// コンテナの出力
template
<class Container>void
dispContainer(const
Container& c) { std::copy(c.begin(), c.end(), std::ostream_iterator<int
>(std::cout," "
)); std::cout << std::endl; }// 順列の出力
template
<class Container>void
dispPermutation() {// 配列からコンテナを生成して・・・
int
ary[] = {1
,2
,3
};const
int
SIZE =sizeof
(ary)/sizeof
(ary[0
]); Container c(ary, &ary[SIZE]);// 順列を出力する
dispContainer(c);while
(std::next_permutation(c.begin(), c.end())) dispContainer(c); std::cout << std::endl; }int
main() { std::cout <<"std::vector<int>"
<< std::endl; dispPermutation<std::vector<int
> >(); std::cout <<"std::list<int>"
<< std::endl; dispPermutation<std::list<int
> >(); std::cout <<"std::set<int>"
<< std::endl; dispPermutation<std::set<int
> >(); std::cout <<"std::string"
<< std::endl; dispPermutation<std::string>(); }