推动分裂与单个字符或只是一个字符串 [英] boost split with a single character or just one string

查看:153
本文介绍了推动分裂与单个字符或只是一个字符串的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在一个字符或字符串分割字符串。我想用的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).

在单个字符情况下,我可以做拆分(VEC,STR,is_any_of(':')),但我想知道是否有办法仅指定一个字符。它可以提高性能,但更重要的是我觉得code将只是一个单个字符清晰,因为 is_any_of 传达了一个不同的含义,我想要的东西。

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.

有关针对字符串匹配,我不知道是什么语法使用。我不希望构建一个正则表达式;一些简单的语法像拆分(VEC,STR,match_str(::)将是一件好事。

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.

推荐答案

在以下code,让我假设使用空间boost 为简洁。结果
至于对字符分割,如果只算法/串是允许的,
is_from_range 可能达到目的:

In the following code, let me assume using namespace boost for brevity.
As for splitting on a character, if only algorithm/string is allowed, is_from_range might serve the purpose:

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

另外,被允许如果的λ

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

如果preparing专用predicate是允许的:

or if preparing a dedicated predicate is allowed:

struct match_char {
  char c;
  match_char(char c) : c(c) {}
  bool operator()(char x) const { return x == c; }
};

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

至于对一个字符串匹配,为的大卫Rodri'guez 的提到,
那里似乎不是与拆分的方式。
如果 iter_split 是允许的,可能是下面的code见面会的宗旨是:

As for matching against a string, as David Rodri'guez mentioned, there seems not to be the way with split. If iter_split is allowed, probably the following code will meet the purpose:

iter_split(vec,str, first_finder("::"));

这篇关于推动分裂与单个字符或只是一个字符串的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆