fork download
  1. #include <algorithm>
  2. #include <iostream>
  3. #include <vector>
  4.  
  5. struct Position {int x; int y;};
  6.  
  7. bool operator == (const Position& lhs, const Position& rhs)
  8. {
  9. return lhs.x == rhs.x && lhs.y == rhs.y;
  10. }
  11.  
  12. bool operator < (const Position& lhs, const Position& rhs)
  13. {
  14. if (lhs.x != rhs.x) {
  15. return lhs.x < rhs.x;
  16. }
  17. return lhs.y < rhs.y;
  18. }
  19.  
  20. std::ostream& operator << (std::ostream& stream, const Position& p)
  21. {
  22. return stream << "(" << p.x << ", " << p.y << ")";
  23. }
  24.  
  25. void print(const std::vector<Position>& v)
  26. {
  27. const char* sep = "";
  28. std::cout << "{";
  29. for (const auto& p : v) {
  30. std::cout << sep << p;
  31. sep = ", ";
  32. }
  33. std::cout << "}" << std::endl;
  34. }
  35.  
  36. void filter(std::vector<Position>& positions, const std::vector<Position>& destroyedFields)
  37. {
  38. for (auto it = positions.begin(); it != positions.end(); ) {
  39. if (std::find(destroyedFields.begin(), destroyedFields.end(), *it) != destroyedFields.end()) {
  40. it = positions.erase(it);
  41. } else {
  42. ++it;
  43. }
  44. }
  45. }
  46.  
  47. std::vector<Position> filter2(const std::vector<Position>& positions, const std::vector<Position>& destroyedFields)
  48. {
  49. std::vector<Position> res;
  50. std::set_difference(positions.begin(), positions.end(),
  51. destroyedFields.begin(), destroyedFields.end(),
  52. std::back_inserter(res));
  53. return res;
  54. }
  55.  
  56. int main()
  57. {
  58. const std::vector<Position> original_positions = {{0, 0}, {1, 3}, {2, 4}, {5, 5}, {42, 0}};
  59. const std::vector<Position> original_destroyedFields = {{2, 4}, {42, 0}};
  60.  
  61. std::vector<Position> positions(original_positions);
  62. std::vector<Position> destroyedFields(original_destroyedFields);
  63.  
  64. std::sort(positions.begin(), positions.end());
  65. std::sort(destroyedFields.begin(), destroyedFields.end());
  66.  
  67. print(filter2(positions, destroyedFields));
  68. filter(positions, destroyedFields);
  69. print(positions);
  70.  
  71. return 0;
  72. }
  73.  
Success #stdin #stdout 0s 3476KB
stdin
Standard input is empty
stdout
{(0, 0), (1, 3), (5, 5)}
{(0, 0), (1, 3), (5, 5)}