fork(3) download
  1. #include <algorithm> // for std::remove_if()
  2. #include <iostream> // for std::cout, std::endl
  3. #include <string> // for std::string
  4. #include <vector> // for std::vector
  5. using namespace std;
  6.  
  7. void print(const char* name, const vector<string>& v);
  8.  
  9. int main()
  10. {
  11. // Input vectors
  12. vector<string> a = {"the", "of"};
  13. vector<string> b = {"oranges", "the", "of", "apples"};
  14.  
  15. print("a", a);
  16. print("b", b);
  17.  
  18. // Use the erase-remove idiom
  19. a.erase(
  20. remove_if(
  21. a.begin(),
  22. a.end(),
  23.  
  24. // This lambda returns true if current string 's'
  25. // (from vector 'a') is in vector 'b'.
  26. [&b](const string& s)
  27. {
  28. auto it = find(b.begin(), b.end(), s);
  29. return (it != b.end());
  30. }
  31. ),
  32.  
  33. a.end()
  34. );
  35.  
  36. cout << "\nAfter removing:\n";
  37. print("a", a);
  38. }
  39.  
  40.  
  41. void print(const char* name, const vector<string>& v)
  42. {
  43. cout << name << " = {";
  44. bool first = true;
  45. for (const auto& s : v)
  46. {
  47. if (first)
  48. {
  49. first = false;
  50. cout << s;
  51. }
  52. else
  53. {
  54. cout << ", " << s;
  55. }
  56. }
  57. cout << "}" << endl;
  58. }
  59.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
a = {the, of}
b = {oranges, the, of, apples}

After removing:
a = {}