The code:
#include <iostream>
#include <boost/algorithm/string.hpp>
#include <boost/utility/string_ref.hpp>
int main()
{
boost::string_ref str = "test_the_world";
std::vector<boost::string_ref> strs;
boost::split(strs, str, boost::is_any_of("_"), boost::token_compress_on);
for (auto& v : strs)
{
std::cout << v << std::endl;
}
return 0;
}
Error:
1>C:\boost_1_55_0\boost/range/iterator_range_core.hpp(643): error C2665: 'boost::basic_string_ref<char,std::char_traits<char>>::basic_string_ref' : none of the 4 overloads could convert all the argument types
1> C:\boost_1_55_0\boost/utility/string_ref.hpp(79): could be 'boost::basic_string_ref<char,std::char_traits<char>>::basic_string_ref(const charT *,boost::basic_string_ref<charT,std::char_traits<char>>::size_type)'
1> with
1> [
1> charT=char
1> ]
1> while trying to match the argument list '(const char *, const char *)'
1> C:\boost_1_55_0\boost/algorithm/string/detail/util.hpp(97) : see reference to function template instantiation 'SeqT boost::copy_range<SeqT,boost::iterator_range<const char *>>(const Range &)' being compiled
1> with
1> [
1> SeqT=boost::basic_string_ref<char,std::char_traits<char>>
1> , Range=boost::iterator_range<const char *>
1> ]
1> C:\boost_1_55_0\boost/algorithm/string/detail/util.hpp(96) : while compiling class template member function 'boost::basic_string_ref<char,std::char_traits<char>> boost::algorithm::detail::copy_iterator_rangeF<boost::basic_string_ref<char,std::char_traits<char>>,input_iterator_type>::operator ()(const boost::iterator_range<const char *> &) const'
Etc .. how can I make this work? Can't see why it shouldn't work?
Boost iter_split
is well suited for this, although it naturally favours iterator_range
instead of string_ref
: Difference between boost::split vs boost::iter_split
using R = boost::iterator_range<std::string::const_iterator>;
using V = std::vector<R>;
V v;
for (auto&& r : iter_split(v, input, token_finder(is_any_of(";"))))
std::cout << r << "\n";
If you insist you can adapt the result:
for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";")))
| transformed([](R const& r){return boost::string_ref(&*r.begin(), r.size());})
std::cout << r << "\n";
Or if you don't have c++11 support:
for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";")))
| transformed(phx::construct<boost::string_ref>(&*phx::begin(_1), phx::size(_1))))
std::cout << r << "\n";
Combine with copy_range<vector<string_ref> >()
if you want a real vector of string_ref
objects.
See it Live On Coliru
#include <boost/algorithm/string.hpp>
#include <boost/algorithm/string/split.hpp>
#include <boost/algorithm/string/predicate.hpp>
#include <boost/algorithm/string/finder.hpp>
#include <boost/range/adaptor/transformed.hpp>
#include <boost/phoenix.hpp>
#include <boost/phoenix/stl.hpp>
#include <boost/utility/string_ref.hpp>
#include <iostream>
using namespace boost::algorithm;
namespace phx = boost::phoenix;
using namespace phx::arg_names;
using boost::adaptors::transformed;
int main() {
std::string input = "1;3;5;7";
using boost::string_ref;
using R = boost::iterator_range<std::string::const_iterator>;
using V = std::vector<R>;
V v;
// just the iterator ranges:
for (auto&& r : iter_split(v, input, token_finder(is_any_of(";"))))
std::cout << r << "\n";
// using a lambda to create the string_refs:
for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";")))
| transformed([](R const& r){return string_ref(&*r.begin(), r.size());}))
std::cout << r << "\n";
// c++03 version:
for (string_ref&& r : iter_split(v, input, token_finder(is_any_of(";")))
| transformed(phx::construct<string_ref>(&*phx::begin(_1), phx::size(_1))))
std::cout << r << "\n";
}
Cheap hack: Specialize copy_range
for string_ref
, because it doesn't take an iterator pair.
namespace boost
{
template <>
inline string_ref
copy_range<string_ref, iterator_range<char const*>>
( iterator_range<char const*> const& r )
{
return string_ref( begin(r), end(r) - begin(r) );
}
}
Demo.
However, there are certainly far better solutions to this, so I'll keep looking.
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