0

i am trying to remove all Orders that have old pizza than 3 days :

i have this vector of pairs :

 std::vector<std::pair<Client,Order>> x;
    x.push_back(std::make_pair(Client(2,"Anca"),Order(3,1)));
    x.push_back(std::make_pair(Client(16,"Maria"),Order(1,3)));
    x.push_back(std::make_pair(Client(29,"Alex"),Order(10,5)));

and this class Order:

class Order{
private:
    int amountPizza;
    int pizzaAge;
public:
int getPizzaAge(){
        return pizzaAge;
    }

and i did something like this :

auto it=x.begin();
   while(it!=x.end()){
        if((it->second).getPizzaAge()>3){
            x.erase(std::remove(x.begin(),x.end(),it->second),x.end());
        }
        it++;
    }

and is not working.

Errors:

error: no match for 'operator==' (operand types are 'std::pair<Client, Order>' and 'const Order')
  { return *__it == _M_value; }
 'std::pair<Client, Order>' is not derived from 'const __gnu_cxx::__normal_iterator<_IteratorL, _Container>'
  { return *__it == _M_value; }
Ac1234
  • 67
  • 6

1 Answers1

2

Using extra while loop is wrong. The moment you erase first element, end() iterator is invalidated. Fortunately, std::remove (and std::remove_if) can handle any number of elements in a single call.

The second issue is that std::remove can only be used for removing exactly same elements (compared with operator ==). However, using std::remove_if you can provide a comparison function that is more flexible.

Your code should look like this:

auto newEnd = std::remove_if(x.begin(), x.end(), [](const auto& p) {
    return p.second.getPizzaAge() > 3;
});
x.erase(newEnd, x.end());
Yksisarvinen
  • 18,008
  • 2
  • 24
  • 52
  • I have tried this and it's working but it's removing all pair,i want to remove only the Order (Order(10,5)) ,but Client to remain (Client(29,"Alex")).It is possible to do this ? – Ac1234 May 06 '20 at 14:09
  • @loveProgram You can't remove part of a pair, since that wouldn't be a pair any more. If you want the order to be optional, use `std::optional`. – molbdnilo May 06 '20 at 14:12
  • It's not. Type of `x` is `std::vector>` , which means for every `Client` there is always an `Order`. If you want clients without orders, you need to change the type of `x` accordingly. If there is at most one order, you can use `std::vector>>` or if there may be more than one Order you may consider `std::vector>>` (or a similar `std::map`, but there is no `std::remove_if` for `std::map`, you will have to write a loop) – Yksisarvinen May 06 '20 at 14:14