fork download
  1. #include <algorithm>
  2. #include <vector>
  3. #include <memory>
  4. #include <iostream>
  5. #include <array>
  6. #include <iterator>
  7.  
  8. template<class pred_type>
  9. struct iter_pred {
  10. pred_type p;
  11. iter_pred(const pred_type& p_) :p(p_) {}
  12. template<class in_iter>
  13. bool operator()(in_iter lhs, in_iter rhs)
  14. {return p(*lhs, *rhs);}
  15. };
  16.  
  17. template<class in_iter, class out_iter, class pred_type>
  18. out_iter sorted_copy(const in_iter& first, const in_iter& last, out_iter dest, const pred_type& pred) {
  19. std::vector<in_iter> tmp;
  20. for(in_iter i=first; i!=last; ++i)
  21. tmp.push_back(i);
  22. std::sort(tmp.begin(), tmp.end(), iter_pred<pred_type>(pred));
  23. for(auto j=tmp.begin(); j!=tmp.end(); ++j)
  24. *(dest++) = **j;
  25. return dest;
  26. }
  27.  
  28. template<class in_iter, class out_iter>
  29. out_iter sorted_copy(const in_iter& first, const in_iter& last, out_iter dest)
  30. { return sorted_copy(first, last, dest, std::less<decltype(*first)>()); }
  31.  
  32. int main() {
  33. std::array<int, 9> src = { { 5, 8, 2, 4, 9, 1, 9, 6, 0 } };
  34. std::vector<int> dst;
  35. sorted_copy(src.begin(), src.end(), std::back_inserter(dst));
  36. std::copy(dst.begin(), dst.end(), std::ostream_iterator<int>(std::cout, " "));
  37. return 0;
  38. }
Success #stdin #stdout 0s 2964KB
stdin
Standard input is empty
stdout
0 1 2 4 5 6 8 9 9