mardi 16 février 2021

Is this redundant use of std::vector::erase and std::remove_if? [duplicate]

I don't usually use std::remove_if in my work, and some example code from a framework we're using shows the below example code for, presumably, removal of "closed task objects" from a vector of task objects:

(task_sessions is a std::vector of "task_session")

task_sessions.erase(
        std::remove_if(task_sessions.begin(),task_sessions.end(),
                    [](const shared_ptr<task_session> &ts) {
                                return ts->is_closed();
                        }),
                    task_sessions.end()
);

As I said, I don't usually use std::remove_if, but it looks to me like the above use if std::remove_if does the real work of removing the closed tasks... but what is the enveloping task_sessions.erase() doing? Won't task_sessions.erase() receive the iterator task_sessions.end() as parameter 1 as well as task_sessions.end() as the 2nd parameter, effectively making the task_sessions.erase() a nop?

Am I missing some implied side effect or something?

Aucun commentaire:

Enregistrer un commentaire