如何使用“删除”功能 std::remove_if 之后的元素

发布于 2024-07-13 06:08:59 字数 911 浏览 3 评论 0原文

假设我们有:

struct IsEven {
   bool operator() (int i) { return i % 2 == 0; }
};

那么:

vector<int> V; // fill with ints
vector<int>::iterator new_end = remove_if(V.begin(), V.end(), IsEven());
V.erase(new_end, V.end());

工作正常(它只留下 V 奇数整数)。 但似乎从 new_endV.end() 的元素不是我们要删除的偶数整数。 例如,如果 v 开头为 1 4 2 8 5 7,那么我会得到这些元素的 8 5 7 (尽管之后erase 调用,向量确实还剩下 1 5 7)。

显然,(根据 http://www.sgi.com/tech/stl/remove_if .html

The iterators in the range [new_last, last) are all still dereferenceable,
but the elements that they point to are unspecified.

首先,WTF? 其次,如何在不重新实现 remove_if 的情况下解决这个问题?

Say we've got:

struct IsEven {
   bool operator() (int i) { return i % 2 == 0; }
};

Then:

vector<int> V; // fill with ints
vector<int>::iterator new_end = remove_if(V.begin(), V.end(), IsEven());
V.erase(new_end, V.end());

works fine (it leaves V with only the odd integers). But it seems that the elements from new_end to V.end() are not the even integers that we're deleting. For example, if v starts out as 1 4 2 8 5 7, then I'm getting 8 5 7 for those elements (although after the erase call, the vector indeed has 1 5 7 left).

Apparently, (according to http://www.sgi.com/tech/stl/remove_if.html)

The iterators in the range [new_last, last) are all still dereferenceable,
but the elements that they point to are unspecified.

First of all, WTF? And second, how do I get around this without essentially reimplementing remove_if?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

感性 2024-07-20 06:08:59

听起来您想使用 partition() 将向量划分为开头为奇数值、结尾为偶数值的组。 partition() 将返回一个指向第二个分组的第一个元素的迭代器。

至于 WTF,我不确定为什么您会期望删除操作通过将要删除的元素复制(这是额外的工作)到容器的末尾来保留它们。
大多数人认为 remove() (及其近亲)中的 WTF 是这样的事实:向量的大小没有减小,并且您必须调用 erase() 来实际上在删除操作后删除不需要的元素。

It sounds like you want to use partition() to partition the vector into groups of odd values at the start and even values at the end. partition() will return an iterator to the first element of the second grouping.

As for the WTF, I'm not sure why you would expect a remove operation to preserve the elements you want to remove by copying them (that's extra work) to the end of the container.
Most people consider the WTF in remove() (and it's cousins) to be the fact that the size of the vector is not reduced and you have to call erase() to actually delete undesired elements after the remove operation.

骄兵必败 2024-07-20 06:08:59

如果您确实想使用“已删除”元素,则需要 std::partition。

If you really want to use the "removed" elements, you want std::partition.

烟花易冷人易散 2024-07-20 06:08:59

我想重点是该函数被称为 remove_if 是有原因的。 它删除元素。 它不会移动它们或选择它们。 调用remove_if后,您不再保证删除的元素存在。 您可以保证的是 firstnew_last 之间的元素不包含任何已删除的元素。

std::partition 会是更好的选择,不是吗? 或者可能是 remove_copy_if,具体取决于您想要执行的操作。

I suppose the point is that the function is called remove_if for a reason. It removes elements. It doesn't move them or select them. After you've called remove_if, you're no longer guaranteed that the elements you removed exist. All you're guaranteed is that the elements between first and new_last do not contain any of the removed elements.

std::partition would be a better choice, wouldn't it? Or perhaps remove_copy_if, depending on exactly what you're trying to do.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文