fork download
  1. #include <iostream>
  2. #include <vector>
  3. #include <algorithm>
  4. #include <functional>
  5. #include <string>
  6. using namespace std;
  7. const int CAPACITY = 5;
  8.  
  9. template <typename t>
  10. std::vector<t> inter(const std::vector <t> & v1, const std::vector <t> & v2)
  11. {
  12. vector <t> v3;
  13. std::set_intersection(v1.begin(), v1.end(), v2.begin(), v2.end(), std::back_inserter(v3));
  14. return v3;
  15. }
  16.  
  17. int main()
  18. {
  19. vector<string> vec1;
  20. string a;
  21. cout << "Enter five stings for vector 1 \n"<< endl;
  22. vec1.push_back("dog");
  23. vec1.push_back("cat");
  24. vec1.push_back("lizard");
  25. vec1.push_back("snake");
  26. vec1.push_back("pig");
  27.  
  28.  
  29. vector<string> vec2;
  30. string b;
  31. cout << "Enter five stings for vector 2 \n"<< endl;
  32. vec2.push_back("cat");
  33. vec2.push_back("sheep");
  34. vec2.push_back("cow");
  35. vec2.push_back("snake");
  36. vec2.push_back("fish");
  37.  
  38. std::sort(vec1.begin(), vec1.end());
  39. std::sort(vec2.begin(), vec2.end());
  40. std::vector<std::string> v3 = inter(vec1, vec2);
  41.  
  42. for(auto &it: v3)
  43. {
  44. cout << it << endl;
  45. }
  46.  
  47. return 0;
  48. }
Success #stdin #stdout 0s 2992KB
stdin
Standard input is empty
stdout
Enter five stings for vector 1 

Enter five stings for vector 2 

cat
snake