汇总打印集合的方法
1. for_each
template<class T> struct display { void operator()(const T& val){ cout<<val<<' '; } }; int main() { vector<int> vec(10); iota(vec.begin(),vec.end(),1); random_shuffle(vec.begin(),vec.end()); for_each(vec.begin(),vec.end(),display<int>()); cout<<endl; }
2. copy
int main() { vector<int> vec(10); iota(vec.begin(),vec.end(),1); random_shuffle(vec.begin(),vec.end()); copy(vec.begin(),vec.end(),ostream_iterator<int>(cout," ")); cout<<endl; }