fork download
  1. #include <algorithm> // for std::set_difference(), std::sort()
  2. #include <iostream> // for std::cout, std::endl
  3. #include <iterator> // for std::inserter
  4. #include <string> // for std::string
  5. #include <vector> // for std::vector
  6. using namespace std;
  7.  
  8. void print(const char* name, const vector<string>& v);
  9.  
  10. int main()
  11. {
  12. // Input vectors
  13. vector<string> a = {"the", "of"};
  14. vector<string> b = {"oranges", "the", "of", "apples"};
  15.  
  16. print("a", a);
  17. print("b", b);
  18.  
  19. // Sort the vectors before calling std::set_difference().
  20. sort(a.begin(), a.end());
  21. sort(b.begin(), b.end());
  22.  
  23. // Resulting difference vector
  24. vector<string> c;
  25. set_difference(a.begin(), a.end(),
  26. b.begin(), b.end(),
  27. inserter(c, c.begin()));
  28.  
  29. print("difference(a,b)", c);
  30. }
  31.  
  32.  
  33. void print(const char* name, const vector<string>& v)
  34. {
  35. cout << name << " = {";
  36. bool first = true;
  37. for (const auto& s : v)
  38. {
  39. if (first)
  40. {
  41. first = false;
  42. cout << s;
  43. }
  44. else
  45. {
  46. cout << ", " << s;
  47. }
  48. }
  49. cout << "}" << endl;
  50. }
  51.  
Success #stdin #stdout 0s 3436KB
stdin
Standard input is empty
stdout
a = {the, of}
b = {oranges, the, of, apples}
difference(a,b) = {}