C++ 使用具有不同参数的Boost标记器转义\u列表\u分隔符

C++ 使用具有不同参数的Boost标记器转义\u列表\u分隔符,c++,string,boost,tokenize,C++,String,Boost,Tokenize,您好,我一直在尝试使用boost library标记器类使标记器工作。 我在boost文档中找到了本教程: 问题是我无法获取参数的转义_list _分隔符(“,”,”) 但是如果我修改boost/tokenizer.hpp文件,它就可以工作了。 但这并不是理想的解决方案,理想的解决方案是想知道我是否缺少任何东西来将不同的参数放入转义列表分隔符中 我想让它在带有“and”的空格上拆分,用于转义,在带引号的字符串中没有转义字符 这用于ingame控制台系统中的参数解析系统。 include &l

您好,我一直在尝试使用boost library标记器类使标记器工作。 我在boost文档中找到了本教程:

问题是我无法获取参数的转义_list _分隔符(“,”,”)

但是如果我修改boost/tokenizer.hpp文件,它就可以工作了。 但这并不是理想的解决方案,理想的解决方案是想知道我是否缺少任何东西来将不同的参数放入转义列表分隔符中

我想让它在带有“and”的空格上拆分,用于转义,在带引号的字符串中没有转义字符

这用于ingame控制台系统中的参数解析系统。

include <iostream>
include <boost/tokenizer.hpp>
include <string>

int main() { using namespace std; using namespace boost; string s = "exec script1 \"script argument number one\""; string separator1("");//dont let quoted arguments escape themselves string separator2(" ");//split on spaces string separator3("\"\'");//let it have quoted arguments tokenizer<escaped_list_separator<char>(separator1,separator2,separator3)> tok(s); for(tokenizer<escaped_list_separator<char>(separator1,separator2,separator3)>::iterator beg=tok.begin(); beg!=tok.end();++beg) { cout << *beg << "\n"; } }

包括
包括
包括

int main() { 使用名称空间std; 使用名称空间boost; string s=“exec script1\”脚本参数1\”; 字符串分隔符1(“”;//不要让引用的参数自行转义 字符串分隔符2(“”;//在空格上拆分 字符串分隔符3(“\”\”);//让它有带引号的参数 标记器tok(s); for(标记器::迭代器beg=tok.begin();beg!=tok.end();++beg) {
cout看起来您的标记器类型声明不正确

typedef boost::tokenizer< boost::escaped_list_separator<char> > Tokenizer;
boost::escaped_list_separator<char> Separator( '\\', ' ', '\"' );
Tokenizer tok( s, Separator );

for( Tokenizer::iterator iter = tok.begin(); iter != tok.end(); ++iter )
{ cout << *iter << "\n"; }
typedef boost::标记器标记器;
boost::转义\u列表\u分隔符('\\',''\');
标记器tok(s,分离器);
对于(标记器::迭代器iter=tok.begin();iter!=tok.end();++iter)
{cout
使用
boost::escaped_list_separator
separator对象作为其TokenizerFunc的类型化对象。

尝试以下操作:

#include <iostream>
#include <boost/tokenizer.hpp>
#include <string>

int main()
{
    using namespace std;
    using namespace boost;
    string s = "exec script1 \"script argument number one\"";
    string separator1("");//dont let quoted arguments escape themselves
    string separator2(" ");//split on spaces
    string separator3("\"\'");//let it have quoted arguments

    escaped_list_separator<char> els(separator1,separator2,separator3);
    tokenizer<escaped_list_separator<char>> tok(s, els);

    for(tokenizer<escaped_list_separator<char>>::iterator beg=tok.begin(); beg!=tok.end();++beg)
    {
        cout << *beg << "\n";
    }
}
#包括
#包括
#包括
int main()
{
使用名称空间std;
使用名称空间boost;
string s=“exec script1\”脚本参数1\”;
字符串分隔符1(“”;//不要让引用的参数自行转义
字符串分隔符2(“”;//在空格上拆分
字符串分隔符3(“\”\”);//让它有带引号的参数
转义\u列表\u分隔符els(分隔符1、分隔符2、分隔符3);
标记器tok(s,els);
for(标记器::迭代器beg=tok.begin();beg!=tok.end();++beg)
{

一个相关的问题是,如果用户想要在结果中输出双引号,维基百科()中描述的嵌入引号(“”)应该替换为
(\\\”
,其中
\\
表示转义字符和
\”
表示引号。这样,引号将显示在输出结果中。 下面的代码片段就是一个示例

typedef boost::escaped_list_separator<char> std_token;
typedef boost::tokenizer<boost::escaped_list_separator<char>> tokenizer;

std_token a_token( "\\", ",", "\"" );
std::string s = "\"Boost,\\\" C++ Libraries\" ";
tokenizer tok{ s, a_token };
for ( const auto &t : tok )
    std::cout << t << '\n';
typedef boost::转义\u列表\u分隔符std\u令牌;
typedef boost::标记器标记器;
std\U标记a\U标记(“\\”,“,”,“\”);
STD::String S=“\”Boost,\“C++库”;
标记器tok{s,a_-token};
用于(const auto&t:tok)

std::cout这些应该是构造函数中的单引号-双引号用于char*,而不是char。谢谢,这确实有效,我知道这是一个小问题:P.可以在输出中保留
,例如
exec | script1 |”脚本参数1“
?@Ferruccio是否有充分的理由不使用
分隔符1(\\”
?”,该分隔符允许对字符串中的引号字符进行转义。@mgd-不,没有。我的回答只是解决了编译错误的原因(错误放置的
)。我保留了所有其他内容。请将其作为您的接受答案(使用您喜欢的答案左侧的复选标记按钮),而不是编辑以指示您喜欢的答案。