加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c – boost :: split在字符串的开始和结尾留空空标志 – 这是期

发布时间:2020-12-16 05:56:21 所属栏目:百科 来源:网络整理
导读:由于我在 documentation年找不到任何东西,所以我以为我在这里问.我有以下程序(C 11): #include iostream #include boost/algorithm/string.hppusing namespace std;using namespace boost;int main () { string tmp = " #tag #tag1#tag2 #tag3 ####tag4 ";
由于我在 documentation年找不到任何东西,所以我以为我在这里问.我有以下程序(C 11):
#include <iostream> 
#include <boost/algorithm/string.hpp>

using namespace std;
using namespace boost;

int main () {
    string tmp = " #tag #tag1#tag2  #tag3 ####tag4   ";
    list<iterator_range<string::iterator> > matches;
    split( matches,tmp,is_any_of("t #"),token_compress_on );

    for( auto match: matches ) {
            cout << "'" << match << "'n";
    }
}

输出为:

''
'tag'
'tag1'
'tag2'
'tag3'
'tag4'
''

我会认为token_compress_on选项删除所有空的令牌.
例如,解决方案是使用boost :: trim_if.然而,我想知道这是否是boost :: split所需的行为,为什么会发生这种情况?

(g 4.6.3,升压1.48)

解决方法

行为是故意的,因为您可以从拆分版本重新创建字符串(包括起始和尾随空格). Boost不知道这个空格是否对您有重要意义(可能是因为某些文件格式可能会强制引导空格/特定的空格).

如果您需要删除前导/尾随空格,您应该按照您的要求修剪或修剪.

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读