在字符串C ++ Boost问题中替换空格

编程入门 行业动态 更新时间:2024-10-28 18:22:33
本文介绍了在字符串C ++ Boost问题中替换空格的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我正在使用此代码替换while空间,但它永远不会替换所有空白,您能告诉我我错了吗

i am using this code to replace the while space but it never replaces all the white space can u please tell me where i am wrong

#include <iostream> #include <string> #include <boost/regex.hpp> #include<boost/algorithm/string.hpp> #include<boost/algorithm/string/replace.hpp> using namespace std; int main(void) { string s4="Hai ad asdasd asdasd"; cout << boost::algorithm::replace_all_copy(s4, " ", " "); }

推荐答案

如果要用单个空格替换任意数量的空格,则可以使用标准的unique(_copy)算法来做到这一点:

If the idea is to replace any number of spaces with a single space, then the standard unique(_copy) algorithm can be tricked to do that:

#include <iostream> #include <string> #include <algorithm> #include <iterator> #include <boost/lambda/lambda.hpp> std::string remove_excessive_spaces(const std::string& s) { using namespace boost::lambda; std::string result; //consider consequtive chars equal only if they are both spaces. unique_copy(s.begin(), s.end(), back_inserter(result), _1 == ' ' && _2 == ' '); return result; } int main(void) { std::string s4="Hai ad asdasd asdasd"; std::cout << remove_excessive_spaces(s4); }

更多推荐

在字符串C ++ Boost问题中替换空格

本文发布于:2023-06-09 06:27:58,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/596839.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:空格   字符串   Boost

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!