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

c – boost :: split在字符串的开始和结尾留空空标志 – 这是期望的行为吗?,第1张

概述由于我在 documentation年找不到任何东西,所以我以为我在这里问.我有以下程序(C 11): #include <iostream> #include <boost/algorithm/string.hpp>using namespace std;using namespace boost;int main () { string tmp = " #tag #tag1 由于我在 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不知道这个空格是否对您有重要意义(可能是因为某些文件格式可能会强制引导空格/特定的空格).

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

总结

以上是内存溢出为你收集整理的c – boost :: split在字符串的开始和结尾留空空标志 – 这是期望的行为吗?全部内容,希望文章能够帮你解决c – boost :: split在字符串的开始和结尾留空空标志 – 这是期望的行为吗?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/langs/1251085.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-06-07
下一篇 2022-06-07

发表评论

登录后才能评论

评论列表(0条)

保存