【发布时间】:2019-11-13 12:23:01
【问题描述】:
我们可以通过流行的erase–remove idiom 从容器中删除一个元素/条目。 但是,我们中的许多人在应用这个成语时会遇到一些问题:
-
一个人很容易陷入错别字之类的陷阱
c.erase(std::remove_if(c.begin(), c.end(), pred)); // , c.end() //---> missing here或
c.erase((std::remove_if(c.begin(), c.end(), pred), c.end())) // ^^ ^^ // extra () makes it pass only c.end() to the c.erase - 它甚至遵循错误语义的容器,如
std::list通过不选择自己的成员std::list::remove_if()为成语。 - 第三,使用
std::remove_ifdoes not work for associative containers。
在c++17 的范围内,我们有没有比std::erase-std::remove_if 或类似std::erase_if 的东西更通用且不易出错的东西,或者c++20 中是否会有这样的实用程序?
【问题讨论】:
标签: c++17 c++20 c++ c++17 c++-standard-library c++20 erase-remove-idiom