I'd like to split a string into smaller parts using delimiters that are longer than one character. I'm curious if boost::algorithm::split can be used for that. I can not figure out what predicate I should give it to compare strings

#include <vector>
#include <string>
#include <boost/algorithm/string/split.hpp>

std::vector< std::string > Result;
std::string Str("This->is->a->string");
boost::algorithm::split(Result, Str, comare_with("->")); // ???

What this comare_with should be?

Thanks a lot.

boost::algorithm::split works like std::strtok . delimiters that are just single characters.
use boost::algorithm::split_regex to split character sequences where delimiters are regular expressions.
for example, to split a string on delimiters which are either sequences of some number of digits or ->

#include <string>
#include <iostream>
#include <iterator>
#include <boost/regex.hpp>
#include <boost/algorithm/string/regex.hpp>
#include <vector>
using namespace std;
using namespace boost;

int main()
  string str = "abc1234def->ijkl->mnop56qrs->tuv7890wxyz" ;
  vector< string > result;
  boost::algorithm::split_regex( result, str, 
                                 regex( "[0-9]+|->" ) ) ;
  copy( result.begin(), result.end(),
        ostream_iterator<string>( cout, "\n" ) ) ;

note: you need to link with libboost_regex. eg.

> g++ -Wall -std=c++98 -pedantic -Werror -I /usr/local/include -L /usr/local/lib -lboost_regex myprogram.cc
Good alternative, clear example
This article has been dead for over six months. Start a new discussion instead.