fork download
  1. #include <iostream>
  2. #include <vector>
  3.  
  4. int main() {
  5. std::vector<int> a{6, 10, 14, 20};
  6. std::vector<int> b{4, 8, 16, 20};
  7.  
  8. std::vector<int> result;
  9. result.reserve(a.size() + b.size());
  10.  
  11. auto aIt = a.cbegin();
  12. auto bIt = b.cbegin();
  13.  
  14. while(aIt != a.cend() && bIt != b.cend())
  15. {
  16. if((aIt != a.end()) && ((*aIt < *bIt) || (bIt == b.end())))
  17. {
  18. if(result.size() == 0 || *aIt != result.back())
  19. {
  20. result.push_back(*aIt);
  21. }
  22. ++aIt;
  23. }
  24. else
  25. {
  26. if(result.size() == 0 || *bIt != result.back())
  27. {
  28. result.push_back(*bIt);
  29. }
  30. ++bIt;
  31. }
  32. }
  33.  
  34. for(int& i : result)
  35. {
  36. std::cout << i << std::endl;
  37. }
  38.  
  39. return 0;
  40. }
Success #stdin #stdout 0s 3464KB
stdin
Standard input is empty
stdout
4
6
8
10
14
16
20