STL remove() 函数的行为 - 仅重新排列容器元素?

发布于 2024-09-09 17:11:37 字数 597 浏览 1 评论 0原文

我在 StackOveflow 和其他来源上读到,删除函数的行为只是对原始容器进行重新排序,以便将要删除的元素移动到容器的末尾并且不会被删除。它们仍然是容器的一部分,remove() 函数只是返回一个迭代器,该迭代器界定要保留的元素范围的末尾。

因此,如果您从未真正修剪掉容器中具有已“删除”值的部分,那么它们应该仍然存在。

但是,当我运行下面的代码时,未“删除”的字母数字字符后面没有尾随空格。

int main()
{
    std::string test "this is a test string with a bunch of spaces to remove";
    remove(test.begin(), test.end(), ' ');
    std::cout << test << std::endl;

    return 0;
}

这是怎么回事?既然我从不调用 test.erase() ,我的字符串上不应该有一堆尾随空格吗?调用remove()后是否保证“已删除”的项目仍然存在?

PS-我不是在寻找有关如何最好地从字符串中删除空格的建议,上面只是令我困惑的remove() 行为的说明性示例。

I've read here on StackOveflow and other sources that the behavior of the remove function is simply re-ordering the original container so that the elements that are TO BE REMOVED are moved to the end of the container and ARE NOT deleted. They remain part of the container and the remove() function simply returns an iterator that delimits the end of the range of elements to keep.

So if you never actually trim off the portion of the container that has the values that have been 'removed', they should still be present.

But when I run the code below there are no trailing spaces after the alphanumeric characters that were not 'removed'.

int main()
{
    std::string test "this is a test string with a bunch of spaces to remove";
    remove(test.begin(), test.end(), ' ');
    std::cout << test << std::endl;

    return 0;
}

What is going on here? Seeing as I never call test.erase() shouldn't I have a bunch of trailing spaces on my string? Is it guaranteed that the 'removed' items will still be present after calling remove()?

PS-I'm not looking for suggestions on how to best remove spaces from a string, the above is simply an illustrative example of the remove() behavior that is confusing me.

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

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

发布评论

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

评论(2

谁的年少不轻狂 2024-09-16 17:11:37

调用 remove 后,留在容器末尾的内容不一定是已删除的元素。这只是垃圾。最有可能的是“在调用删除之前这些位置上的任何内容”,但你也不能依赖它。就像未初始化的变量一样,它可以是任何东西。

例如,字符串:

"Hi I am Bob!\0"

after a call to remove to stop space probable 看起来像这样

"HiIamBob!\0b!\0"

您不会在 cout 上看到它但是,因为一旦到达“\0”,它将停止打印。

What's left at the end of your container after a call to remove is not necessarily the elements that were removed. It's just junk. Most likely it's "whatever was in those positions before the call to remove", but you can't rely on that either. Much like an uninitialized variable, it could be anything.

For example, the string:

"Hi I am Bob!\0"

after a call to remove to get rid of spaces probably looks like this

"HiIamBob!\0b!\0"

You won't see that on a cout, though, because it will stop printing once it hits the '\0'.

小霸王臭丫头 2024-09-16 17:11:37

您可能愿意获取 Boost.String

它是作用于字符串的算法的集合。

boost::erase_all(test, " ");

You might be willing to get Boost.String.

It's a collection of algorithms to act on strings.

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