fork(3) download
  1. #include <algorithm>
  2. #include <iterator>
  3. #include <string>
  4. #include <vector>
  5. #include <list>
  6. #include <iostream>
  7.  
  8. bool funny_comp(const std::string &lhs, const std::string &rhs)
  9. {
  10. return (lhs.size() == rhs.size()) ? lhs < rhs
  11. : lhs.size() < rhs.size();
  12. }
  13.  
  14. int main()
  15. {
  16.  
  17. std::vector<std::string> vec1{"and", "thus", "it", "has", "a", "beginning", "and", "end"};
  18. std::vector<std::string> vec2{"and", "therefore", "stars", "are", "beginning", "to","fall","to","their", "end"};
  19.  
  20. std::sort(vec1.begin(), vec1.end(), funny_comp);
  21. std::sort(vec2.begin(), vec2.end(), funny_comp);
  22. std::list<std::string> intersection;
  23.  
  24. std::set_intersection(vec1.begin(), vec1.end(),
  25. vec2.begin(), vec2.end(),
  26. std::back_inserter(intersection),
  27. funny_comp);
  28.  
  29. for (const auto& s : intersection)
  30. std::cout << s << " ";
  31. std::cout << std::endl;
  32.  
  33.  
  34. }
  35.  
Success #stdin #stdout 0s 3036KB
stdin
Standard input is empty
stdout
and end beginning