Where is erase_if?

后端 未结 6 1307
不思量自难忘°
不思量自难忘° 2021-01-07 20:51

I\'ve got a container and would like to erase elements based on a predicate. erase_if sounds familiar, but I can\'t find it in C++. What\'s the name and where i

6条回答
  •  暗喜
    暗喜 (楼主)
    2021-01-07 21:25

    I'm guessing you're thinking of remove_if which takes a predicate to determine if the element ought to be removed.

    remove_if returns an iterator pointing to the beginning of the elements to remove in the container. To actually remove them you need to use erase:

    container.erase(remove_if(container.start(), container.end(), pred), container.end())
    

    Either that or perhaps you mistakenly recalled the copy_if algorithm? Which somehow got left out of the standard but was written about - and implemented - in Effective STL.

提交回复
热议问题