使用带有不同参数的 Boost Tokenizer escaped_list_separator

发布于 2024-07-13 09:25:26 字数 1413 浏览 6 评论 0原文

你好,我一直在尝试使用 boost 库 tokenizer 类让 tokenizer 工作。 我在 boost 文档中找到了本教程:

http://www .boost.org/doc/libs/1 _36 _0/libs/tokenizer/escaped _list _separator.htm

问题是我无法获取转义 _list _separator("","",""); 的参数

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

我想让它在空格上分割,用 " 和 ' 进行转义,并且在引用的字符串内没有转义字符。

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

#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";
    }
}

来自 Visual Studio 2005 的错误是 错误 C2974: 'boost::tokenizer' : 'TokenizerFunc' 的模板参数无效,输入预期的

编辑: 这个问题由费鲁西奥提出,并由彼得·谢克的大家解释。

Hello i been trying to get a tokenizer to work using the boost library tokenizer class.
I found this tutorial on the boost documentation:

http://www.boost.org/doc/libs/1 _36 _0/libs/tokenizer/escaped _list _separator.htm

problem is i cant get the argument's to escaped _list _separator("","","");

but if i modify the boost/tokenizer.hpp file it work's.
but that's not and ideal solution was wondering if there's anything i am missing to get diferent arguments into the escaped _list _separator.

i want to make it split on spaces with " and ' for escaping and with no escape character inside the quoted string.

this is used for a argument parsing system in a ingame console system.

#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";
    }
}

the error from visual studio 2005 is
error C2974: 'boost::tokenizer' : invalid template argument for 'TokenizerFunc', type expected

EDIT:
This question was awnsered by ferrucio and explained by peter thank's everybody.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

亚希 2024-07-20 09:25:26

尝试这个:

#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";
    }
}

try this:

#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";
    }
}
笔落惊风雨 2024-07-20 09:25:26

您似乎错误地声明了您的分词器类型。

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"; }

您想要创建一个 boost::tokenizerboost::tokenizer< boost::escaped_list_separator< 字符> > 带有 boost::escaped_list_separator的类型化对象 char > 分隔符对象作为其 TokenizerFunc。

It seems like you're declaring your tokenizer type incorrectly.

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"; }

You want to make a boost::tokenizer< boost::escaped_list_separator< char > > typed object with a boost::escaped_list_separator< char > separator object as its TokenizerFunc.

晨曦÷微暖 2024-07-20 09:25:26

一个相关但不是答案的问题是,如果用户想在结果中输出双引号,则维基百科 (像这里) 应替换为 (\\\") 字符串,其中 \\ 表示转义字符,\" 表示引号。 这样输出结果中就会显示引号。
以下代码片段是一个示例。

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';

这是典型的输出

A relevant point yet not the answer for this is that if user want to output a double quote in the result, the embedded quote ("") described in the Wikipedia (like here) should be replaced with a string of (\\\"), where \\ means a escape char and \" means the quote mark. In this way, the quote mark will be displayed in the output result.
The following code snippet is an example.

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';

This is a typical output for this

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文