gpt4 book ai didi

remove_if 的 C++ 意外行为

转载 作者:行者123 更新时间:2023-12-04 13:45:50 38 4
gpt4 key购买 nike

我正在尝试使用 std::remove_if从一个简单的字符串中删除空格,但我得到了奇怪的结果。有人可以帮我弄清楚发生了什么吗?

该代码是:

#include <iostream>
#include <algorithm>
#include <string>

int main(int argc, char * argv[])
{
std::string test = "a b";
std::remove_if(test.begin(), test.end(), isspace);
std::cout << "test : " << test << std::endl;

return 0;
}

我希望这可以简单地打印出来:
test : ab

但相反我得到
test : abb

尝试使用另一个字符串,我得到:

输入:“a bcde uv xy”

输出:“abcdeuvxy xy”

似乎它复制了最后一个“单词”,但有时会添加一个空格。我怎样才能让它删除所有空格而不做奇怪的事情?

最佳答案

std::remove_if 通过移动元素执行移除;实际上,删除的元素不会从容器中删除。 STL 算法没有这样的特权;只有容器可以移除它们的元素。

(强调我的)

Removing is done by shifting (by means of move assignment) the elements in the range in such a way that the elements that are not to be removed appear in the beginning of the range. Relative order of the elements that remain is preserved and the physical size of the container is unchanged. Iterators pointing to an element between the new logical end and the physical end of the range are still dereferenceable, but the elements themselves have unspecified values (as per MoveAssignable post-condition). A call to remove is typically followed by a call to a container's erase method, which erases the unspecified values and reduces the physical size of the container to match its new logical size.



您可以 erase之后删除的元素(称为erase-remove idiom)。
test.erase(std::remove_if(test.begin(), test.end(), isspace), test.end());

关于remove_if 的 C++ 意外行为,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59098518/

38 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com