Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost string split to eliminate spaces in words

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
like image 265
Vihaan Verma Avatar asked May 11 '12 12:05

Vihaan Verma


2 Answers

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); 
like image 84
Jon Avatar answered Nov 17 '22 10:11

Jon


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

like image 29
Steve Jessop Avatar answered Nov 17 '22 09:11

Steve Jessop