fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3. #include <initializer_list>
  4.  
  5. template<typename T>
  6. std::vector<T> merge(std::initializer_list <std::vector<T>*> vecs)
  7. {
  8. size_t size = 0;
  9. for(auto v : vecs) { size += v->size(); }
  10. std::vector<T> ret;
  11. ret.reserve(size);
  12. for(auto v : vecs) { ret.insert(ret.end(), v->begin(), v->end()); }
  13. return ret;
  14. }
  15.  
  16. int main()
  17. {
  18. std::vector<std::string> a = {"a", "b"};
  19. std::vector<std::string> b = {"c", "d"};
  20. std::vector<std::string> c = {"e", "f"};
  21. std::vector<std::string> d = {"g", "h"};
  22.  
  23. std::vector<std::string> newVector = merge({&a, &b, &c, &d});
  24.  
  25. for(auto &s : newVector)
  26. std::cout << s << " ";
  27.  
  28. return 0;
  29. }
Success #stdin #stdout 0s 15240KB
stdin
Standard input is empty
stdout
a b c d e f g h