fork(2) download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. using namespace std;
  5.  
  6. template <class Container>
  7. void insert_sorted(Container& where, Container& what)
  8. {
  9. typename Container::iterator src = what.begin();
  10. typename Container::iterator src_end = what.end();
  11.  
  12. size_t index = 0;
  13.  
  14. while(src != src_end)
  15. {
  16. if(*src < where[index])
  17. {
  18. where.insert(where.begin() + index, *src);
  19. ++src;
  20. }
  21. ++index;
  22. }
  23. }
  24.  
  25. int main()
  26. {
  27. vector<int> foo{ 0, 5, 7, 9, 11, 14 };
  28. vector<int> bar{ 1, 2, 4, 8, 10, 12 };
  29.  
  30. insert_sorted(foo, bar);
  31.  
  32. for(vector<int>::iterator i = foo.begin(); i != foo.end(); ++i)
  33. {
  34. cout << *i << " ";
  35. }
  36.  
  37. return 0;
  38. }
Success #stdin #stdout 0s 3276KB
stdin
Standard input is empty
stdout
0 1 2 4 5 7 8 9 10 11 12 14