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
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);
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With