Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

boost split with a single character or just one string

Tags:

c++

string

boost

I wish to split a string on a single character or a string. I would like to use boost::split since boost string is our standard for basic string handling (I don't wish to mix several techniques).

In the single character case I could do split(vec,str,is_any_of(':')) but I'd like to know if there is a way to specify just a single character. It may improve performance, but more importantly I think the code would be clearer with just a single character, since is_any_of conveys a different meaning that what I want.

For matching against a string I don't know what syntax to use. I don't wish to to construct a regex; some simple syntax like split(vec,str,match_str("::") would be good.

like image 477
edA-qa mort-ora-y Avatar asked Apr 18 '11 07:04

edA-qa mort-ora-y


1 Answers

I was looking for the same answer but I couldn't find one. Finally I managed to produce one on my own.

You can use std::equal_to to form the predicate you need. Here's an example:

boost::split(container, str, std::bind1st(std::equal_to<char>(), ','));

This is exactly how I do it when I need to split a string using a single character.

like image 165
Adam Romanek Avatar answered Oct 08 '22 02:10

Adam Romanek