开发者

replace whitespace in an string c++ boost problem

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/开发者_Go百科algorithm/string/replace.hpp>

using namespace std;

int main(void)
{
        string s4="Hai ad                     asdasd                       asdasd";
        cout << boost::algorithm::replace_all_copy(s4, "    ", " ");
}


your code replaces every occurence of 4 white spaces with a single white space. Since there are multiple occurences of the 4 white space pattern, you get multiple (single) white spaces in the output again.

I'm not sure what you are trying to achieve. But to erase all white spaces you better use erase_all_copy(). Or to erase all occurences of 4 white spaces, you should follow Potatoswatters advice.

#include <iostream>
#include <string>
#include<boost/algorithm/string/replace.hpp>
#include <boost/algorithm/string/erase.hpp>

using namespace std;

int main(void) {
        string s4="Hai ad                     asdasd                       asdasd";

        cout << boost::algorithm::erase_all_copy( s4, " " ) << endl;
        cout << boost::algorithm::replace_all_copy( s4, "    ", "") << endl;
}

Or is your goal to remove all white spaces between words, except for one?


The stream operators remove extra white space automatically

#include <iostream>
#include <string>
#include <sstream>
#include <iterator>
#include <algorithm>

using namespace std;

int main()  // Note: main(void) is not valid
{
    string s4="Hai ad                     asdasd                       asdasd";


    // Technique 1: Simple
    std::stringstream  s4Stream(s4);

    std::string s5;
    std::string word;
    while(s4Stream >> word)
    {
        s5.append(word).append(" ");
    }
    std::cout << s4 << "\n" << s5 << "\n";


    // Technique2: Using STL algorithms
    std::stringstream s4bStream(s4);
    std::stringstream s5bStream;

    std::copy(std::istream_iterator<std::string>(s4bStream),
              std::istream_iterator<std::string>(),
              std::ostream_iterator<std::string>(s5bStream," ")
             );

    std::string       s5b = s5bStream.str();
    std::cout << s4 << "\n" << s5b << "\n";
}


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);
}


If you want to completely eliminate the whitespace, the final argument should be the empty string.

     cout << boost::algorithm::replace_all_copy(s4, "    ", "");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜