Increase split with one character or just one line

I want to split a string into one character or string. I would like to use boost::split , since the string boost is our standard for basic string processing (I don't want to mix multiple methods).

In the case of a single character, I could do split(vec,str,is_any_of(':')) , but I would like to know if there is a way to specify only one character. This can improve performance, but, more importantly, I think the code will be more clear with one character, since is_any_of passes the other value that I want.

For string matching, I don't know which syntax to use. I do not want to create a regular expression; simple syntax like split(vec,str,match_str("::") would be good.

+7
source share
3 answers

In the following code, let me accept using namespace boost for short.
As for character separation, only algorithm/string is_from_range allowed to serve the purpose:

 split(vec,str, is_from_range(':',':')); 

Alternatively, if lambda allowed:

 split(vec,str, lambda::_1 == ':'); 

or if a dedicated predicate is prepared:

 struct match_char { char c; match_char(char c) : c(c) {} bool operator()(char x) const { return x == c; } }; split(vec,str, match_char(':')); 

Regarding string matching, as David Rodriguez mentioned, there doesn't seem to be a split . If iter_split enabled, perhaps the following code will fit the target:

 iter_split(vec,str, first_finder("::")); 
+3
source

I was looking for the same answer, but I could not find it. Finally, I managed to create it myself.

You can use std::equal_to to form the predicate that you need. Here is 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.

+10
source

In a simple token, I just left is_any_of , as it’s easy to understand what is_any_of( single_option ) means. If you really want to change it, the third element is a functor, so you can pass the equals functor to the split function.

This approach will not work with multiple tokens, since the iteration should be characater in character. I do not know libraries to offer ready-made alternatives, but you can implement functionality on top of split_iterator s

+1
source

All Articles