Force split line to exclude spaces in words

I wrote this code to split a line containing words with many spaces and / or tab into a line vector containing only words.

 1 #include<iostream>                                                              
 2 #include<vector>                                                                
 3 #include<boost/algorithm/string/split.hpp>                                      
 4 #include<boost/algorithm/string.hpp>                                            
 5 int main()                                                                      
 6 {                                                                               
 7     using namespace std;                                                        
 8                                                                                 
 9     string str("cONtainS            SoMe    CApiTaL WORDS");                    
10                                                                                 
11     vector<string> strVec;                                                      
12     using boost::is_any_of;                                                     
13                                                                                 
14     boost::algorithm::split(strVec, str, is_any_of("\t "));                                             
15                                                                                 
16     vector<string>::iterator i ;                                                
17                                                                                 
18     for(i = strVec.begin() ; i != strVec.end(); i++)                            
19         cout<<*i<<endl;                                                         
20                                                                                 
21     return 0;                                                                   
22 }                                                                               
23    

I was expecting a way out

cONtainS
SoMe
CApiTaL
WORDS

but I get output with space as an element in strVec ie

cONtainS











SoMe



CApiTaL
WORDS
+5
source share
2 answers

You need to add the final parameter with the value boost::token_compress_on, according to the documentation :

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

This is because your input contains sequential delimiters. By default it splitinterprets this to keep in mind that there are blank lines between them.

, eCompress token_compress_on.

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

+3

All Articles