在字符串C ++ Boost问题中替换空格 [英] replace whitespace in an string c++ boost problem
本文介绍了在字符串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问题中替换空格的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文