从 C++ 中的 std::string 中删除空格
在 C++ 中从字符串中删除空格的首选方法是什么? 我可以循环遍历所有字符并构建一个新字符串,但有更好的方法吗?
What is the preferred way to remove spaces from a string in C++? I could loop through all the characters and build a new string, but is there a better way?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(19)
来自 gamedev
From gamedev
你能使用Boost字符串算法吗? http://www.boost.org/doc /libs/1_35_0/doc/html/string_algo/usage.html#id1290573
Can you use Boost String Algo? http://www.boost.org/doc/libs/1_35_0/doc/html/string_algo/usage.html#id1290573
您可以使用此解决方案来删除字符:
You can use this solution for removing a char:
对于修剪,请使用 boost 字符串算法:
For trimming, use boost string algorithms:
嗨,你可以做类似的事情。 该函数删除所有空格。
我做了另一个函数,删除所有不必要的空格。
Hi, you can do something like that. This function deletes all spaces.
I made another function, that deletes all unnecessary spaces.
在 C++20 中,您可以使用自由函数 std::erase
完整示例:
I print | 很明显,开头的空格也被删除了。
注意:这仅删除空格,而不删除所有其他可能被视为空格的字符,请参阅 https://en.cppreference.com/w/cpp/string/byte/isspace
In C++20 you can use free function std::erase
Full example:
I print | so that it is obvious that space at the begining is also removed.
note: this removes only the space, not every other possible character that may be considered whitespace, see https://en.cppreference.com/w/cpp/string/byte/isspace
用它:
use it:
如果您想使用一个简单的宏来完成此操作,可以使用以下宏:
当然,这假设您已经完成了
#include
。像这样称呼它:
If you want to do this with an easy macro, here's one:
This assumes you have done
#include <string>
of course.Call it like so:
删除所有 空白字符,例如制表符和换行符 (C++11):
Removes all whitespace characters such as tabs and line breaks (C++11):
资料来源:
参考资料取自此论坛。
Source:
Reference taken from this forum.
我使用了下面的解决方法很长时间 - 不确定它的复杂性。
s.erase(std::unique(s.begin(),s.end(),[](char s,char f){return (f==' '||s==' '); }),s.end());
当你想删除字符
' '
和一些例如-
使用s.erase(std: :unique(s.begin(),s.end(),[](char s,char f){return ((f==''||s=='')||(f=='-' ||s=='-'));}),s.end());
同样,如果您想要删除的字符数不是 1
,而是 增加
||
,其他人提到的擦除删除习语似乎也不错。I used the below work around for long - not sure about its complexity.
s.erase(std::unique(s.begin(),s.end(),[](char s,char f){return (f==' '||s==' ');}),s.end());
when you wanna remove character
' '
and some for example-
uses.erase(std::unique(s.begin(),s.end(),[](char s,char f){return ((f==' '||s==' ')||(f=='-'||s=='-'));}),s.end());
likewise just increase the
||
if number of characters you wanna remove is not 1but as mentioned by others the erase remove idiom also seems fine.
该代码基本上采用一个字符串并迭代其中的每个字符。 然后它检查该字符串是否是空格,如果不是,则将该字符添加到新字符串中。
This code basically takes a string and iterates through every character in it. It then checks whether that string is a white space, if it isn't then the character is added to a new string.
只是为了好玩,因为其他答案比这个好得多。
Just for fun, as other answers are much better than this.
输出:2CF4323CB9DE
output: 2CF4323CB9DE
我创建了一个函数,用于删除字符串两端的空格。 例如
“Hello World”
,将被转换为“Hello world”
。其工作原理类似于 python 中经常使用的
strip
、lstrip
和rstrip
函数。I created a function, that removes the white spaces from the either ends of string. Such as
" Hello World "
, will be converted into"Hello world"
.This works similar to
strip
,lstrip
andrstrip
functions, which are frequently used in python.恐怕这是我能想到的最好的解决方案。 但是您可以使用 Reserve() 提前预先分配所需的最小内存,以加快速度。 您最终会得到一个新字符串,该字符串可能会更短,但占用相同数量的内存,但您将避免重新分配。
编辑:根据您的情况,这可能会比乱七八糟的字符产生更少的开销。
您应该尝试不同的方法,看看什么最适合您:您可能根本不会遇到任何性能问题。
I'm afraid it's the best solution that I can think of. But you can use reserve() to pre-allocate the minimum required memory in advance to speed up things a bit. You'll end up with a new string that will probably be shorter but that takes up the same amount of memory, but you'll avoid reallocations.
EDIT: Depending on your situation, this may incur less overhead than jumbling characters around.
You should try different approaches and see what is best for you: you might not have any performance issues at all.
最好的办法是使用算法
remove_if
和 isspace:现在算法本身无法更改容器(只能修改值),因此它实际上会打乱值并返回一个指向现在应该结束的位置的指针。 因此,我们必须调用 string::erase 来实际修改容器的长度:
我们还应该注意,remove_if 最多只会复制一份数据。 这是一个示例实现:
The best thing to do is to use the algorithm
remove_if
and isspace:Now the algorithm itself can't change the container(only modify the values), so it actually shuffles the values around and returns a pointer to where the end now should be. So we have to call string::erase to actually modify the length of the container:
We should also note that remove_if will make at most one copy of the data. Here is a sample implementation: