fork(4) download
  1. #include <vector>
  2. #include <utility>
  3. #include <iterator>
  4.  
  5. namespace detail
  6. {
  7. template <typename R>
  8. void do_concatenation(R& accum) {}
  9.  
  10. template <typename R, typename First, typename... More>
  11. void do_concatenation(R& accum, First const& first, More const&... more)
  12. {
  13. using std::begin;
  14. using std::end;
  15. std::copy(begin(first), end(first), std::inserter(accum, end(accum)));
  16.  
  17. do_concatenation(accum, more...);
  18. }
  19. }
  20.  
  21. template <typename Result, typename... Containers>
  22. Result concatenate(Containers const&... containers)
  23. {
  24. Result accum;
  25. detail::do_concatenation(accum, containers...);
  26. return accum;
  27. }
  28.  
  29. template <typename First, typename... More>
  30. std::vector<typename First::value_type> to_vector(First const& first, More const&... containers)
  31. {
  32. return concatenate<std::vector<typename First::value_type>>(first, containers...);
  33. }
  34.  
  35. /// demo
  36. #include <set>
  37. #include <list>
  38. #include <iostream>
  39.  
  40. #include <map>
  41. #include <string>
  42.  
  43. int main()
  44. {
  45. auto x = to_vector(std::vector<int> { 1,2,3 }, std::list<int> { 9,8,11 }, std::set<int> { 42 });
  46.  
  47. for (auto i : x)
  48. std::cout << i << " ";
  49.  
  50. std::cout << std::endl;
  51.  
  52. // fun with maps:
  53. auto y = concatenate<std::map<long, std::string> >(
  54. std::map<int, const char*> { { 1, "one" }, { 2, "two" } },
  55. std::map<unsigned, std::string> { { 1, "one" }, { 3, "three" } }
  56. );
  57.  
  58. for (auto kvp : y)
  59. std::cout << "(" << kvp.first << ", " << kvp.second << ")";
  60.  
  61. }
  62.  
Success #stdin #stdout 0s 2996KB
stdin
Standard input is empty
stdout
1 2 3 9 8 11 42 
(1, one)(2, two)(3, three)