众所周知,从 a 中完全删除所需项目的一个好方法std::vector
是擦除删除习语。
如上述链接中所述(截至本文发布之日),在代码中,erase-remove习惯用法如下所示:
int main()
{
// initialises a vector that holds the numbers from 0-9.
std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// erase-remove idiom to completely eliminate the desired items from the vector
v.erase( std::remove( std::begin(v), std::end(v), 5 ), std::end(v) );
}
我想知道一个resize-remove
成语在功能和性能方面是否与成语等效erase-remove
。或者,也许我遗漏了一些明显的东西?
以下resize-remove
成语是否等同于上述erase-remove
成语?
int main()
{
std::vector<int> v = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
// Is this "resize-remove" approach equivalent to the "erase-remove" idiom?
v.resize( std::remove( std::begin(v), std::end(v), 5 ) - v.begin() );
}