帮助我理解 std::erase

发布于 2024-08-13 04:18:30 字数 288 浏览 5 评论 0原文

在《C++ In A Nutshell》一书中,有以下示例代码

std::vector<int> data
...
std::erase(std::remove(data.begin(), data.end(), 42),
  data.end());

我认为“erase”是一个成员函数,所以不应该是“data.erase”而不是“std::erase”吗? C++ 编译器是否有某种方式可以告诉您要调用成员函数的成员,或者本书是否省略了擦除模板函数的任何文档,或者示例是否错误?

In the book 'C++ In A Nutshell', there is the following example code

std::vector<int> data
...
std::erase(std::remove(data.begin(), data.end(), 42),
  data.end());

I thought that 'erase' was a member function, so shouldn't that be 'data.erase' rather than 'std::erase'?
Is there some way the c++ compiler can tell what member you wanted to call a member function on, or did the book omit any documentation of an erase template function, or is the example wrong?

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

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

发布评论

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

评论(6

夏见 2024-08-20 04:18:30

erase 是一个成员函数。提供的样本不正确。

erase is a member function. The sample provided is incorrect.

金兰素衣 2024-08-20 04:18:30

没有std::erasestd::map::erasestd::list::erase 存在。但不存在 std::erase

查看这个问题幻影 std::erase。

There is no std::erase. std::map::erase, std::list::erase exists. But no std::erase exists.

Look this question about phantom std::erase.

坠似风落 2024-08-20 04:18:30

是的,erase 是一个成员函数,所以它应该是 data.erase() 而不是 std::erase()

Yes, erase is a member function, so it should be data.erase() instead of std::erase().

浅唱々樱花落 2024-08-20 04:18:30

你的观察是正确的。 “擦除”应该是一个成员函数。只有容器上的成员函数才能更改该容器的内存大小。

You observation is correct. 'Erase' should be a member function. Only a member function on a container can change the memory size of that container.

药祭#氼 2024-08-20 04:18:30

编辑:抱歉,他们不是通用擦除,只是仔细检查

Edit: Sorry their is no generic erase, just double checked

失与倦" 2024-08-20 04:18:30

有一种称为 std::remove 的算法。并对数据结构调用擦除。 remove 将所有要删除的元素移动到迭代器范围的末尾,并返回要删除的第一个元素。如果找不到元素,则返回 end()。

因此,您可以在从 std::remove 的返回值和数据结构末尾开始的范围内调用擦除。

请参阅:http://www.sgi.com/tech/stl/remove.html< /a>

请注意,remove 不适用于有序数据结构,因为元素无法重新排列。

删除是线性的,因此将从末尾删除一个向量。因为它不需要在要删除的元素之后使元素冒泡。

std::vector<int> data
...
data.erase(std::remove(data.begin(), data.end(), 42), data.end())

与这样的 O(N**2) 相比

std::vector<int> data
...
for ( i = data.begin(), i != data.end(); ++i ) {
  if ( *i == 42 ) data.erase( i ) ;
}

There is an algorithm called std::remove. And calling erase on the data structure. remove moves all the elements to be erased to the end of the iterator range and returns the first element to be removed. Returns end() if the element could not be found.

So then you call erase on range starting with the return value of std::remove and the end of the data structure.

See: http://www.sgi.com/tech/stl/remove.html

Note that remove won't work with ordered datastructures, as the elements can't be rearranged.

remove is linear, and so would be removing a vector from up to the end. As it wouldn't need to bubble up the elements after the elements to be removed.

std::vector<int> data
...
data.erase(std::remove(data.begin(), data.end(), 42), data.end())

compared to something like this which is O(N**2)

std::vector<int> data
...
for ( i = data.begin(), i != data.end(); ++i ) {
  if ( *i == 42 ) data.erase( i ) ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文