Can a unary predicate for remove_if have side effects?

Should these arent modifying the container? For example, I want to deduce all those that I delete from the vector (and I do not want to use several passes: for example: section + output + erasure). Deviation of design from this is legal:

v.erase(remove_if(v.begin(), v.end(), [] (const int i) -> bool 
                                  { 
                                    if (i%2==0)
                                    {
                                       cout << i << endl;
                                       return true;
                                    }
                                    else return false;
                                   }, v.end());

AFAIK standard guarantees exactly one application of the predicate for each element, so I am fine, since I do not care about the order ...

+3
source share
1 answer

The standard really guarantees this, so you're fine. However, I would still consider this a bad style other than debugging.

+3
source

All Articles