boost string split to eliminate spaces in words

20,775

Solution 1

You need to add a final parameter with the value boost::token_compress_on, as per the documentation:

boost::algorithm::split(strVec,str,is_any_of("\t "),boost::token_compress_on); 

Solution 2

It's because your input contains consecutive separators. By default split interprets that to mean they have empty strings between them.

To get the output you expected, you need to specify the optional eCompress parameter, with value token_compress_on.

http://www.boost.org/doc/libs/1_43_0/doc/html/boost/algorithm/split_id667600.html

Share:
20,775
Vihaan Verma
Author by

Vihaan Verma

Updated on July 09, 2022

Comments

  • Vihaan Verma
    Vihaan Verma almost 2 years

    I have written this code to split up a string containing words with many spaces and/or tab into a string vector just containing the words.

    #include<iostream>       
    #include<vector>                        
    #include<boost/algorithm/string/split.hpp>       
    #include<boost/algorithm/string.hpp>                         
    int main()                              
    {                  
        using namespace std;                                     
    
        string str("cONtainS            SoMe    CApiTaL WORDS");       
    
        vector<string> strVec;              
        using boost::is_any_of;       
    
        boost::algorithm::split(strVec, str, is_any_of("\t "));       
    
        vector<string>::iterator i ;       
    
        for(i = strVec.begin() ; i != strVec.end(); i++)       
            cout<<*i<<endl;              
    
        return 0;                                             
    }
    

    I was expecting an output

    cONtainS
    SoMe
    CApiTaL
    WORDS
    

    but i m geting output with space as an element in the strVec i.e

    cONtainS
    
    
    
    
    
    
    
    
    
    
    
    SoMe
    
    
    
    CApiTaL
    WORDS