Discussion:
Splitting string at whitespace
Florian Lindner
2014-06-16 13:34:18 UTC
Permalink
Hello,

for some time now I've been trying to split a given string at whitespaces:
"A B C" -> "A", "B", "C". I want to use boost/algorithm/string

#include "boost/algorithm/string/split.hpp"
#include "boost/algorithm/string/compare.hpp"

std::vector<std::string> tokenize(const std::string& text )
{
std::vector<std::string> tokens;
boost::algorithm::split(tokens, text, boost::algorithm::is_equal(" "));
return tokens;
}

but compilation on gcc 4.9.0 / boost 1.55.0 just gives:



test.cpp: In function 'std::vector<std::basic_string<char> > tokenize(const
string&)':
test.cpp:40:71: error: no matching function for call to
'boost::algorithm::is_equal::is_equal(const char [2])'
boost::algorithm::split(tokens, text, boost::algorithm::is_equal(" "));
^
test.cpp:40:71: note: candidates are:
In file included from /usr/include/boost/algorithm/string/finder.hpp:24:0,
from /usr/include/boost/algorithm/string/split.hpp:17,
from test.cpp:7:
/usr/include/boost/algorithm/string/compare.hpp:34:16: note:
boost::algorithm::is_equal::is_equal()
struct is_equal
^
/usr/include/boost/algorithm/string/compare.hpp:34:16: note: candidate
expects 0 arguments, 1 provided
/usr/include/boost/algorithm/string/compare.hpp:34:16: note:
boost::algorithm::is_equal::is_equal(const boost::algorithm::is_equal&)
/usr/include/boost/algorithm/string/compare.hpp:34:16: note: no known
conversion for argument 1 from 'const char [2]' to 'const
boost::algorithm::is_equal&'


What is wrong there?

Thanks,
Florian
Marshall Clow
2014-06-17 09:49:49 UTC
Permalink
Post by Florian Lindner
Hello,
"A B C" -> "A", "B", "C". I want to use boost/algorithm/string
#include "boost/algorithm/string/split.hpp"
#include "boost/algorithm/string/compare.hpp"
std::vector<std::string> tokenize(const std::string& text )
{
std::vector<std::string> tokens;
boost::algorithm::split(tokens, text, boost::algorithm::is_equal(" "));
return tokens;
}
That’s the wrong predicate to use; use is_space instead.
boost::algorithm::split(tokens, text, boost::algorithm::is_space());

is_equal takes two characters and says if they’re equal.
split wants to call the predicate with a single character and get a true/false back.
[snip]

— Marshall

Loading...