的boost ::精神::引号中的字符串输出因缘 [英] boost::spirit::karma output of string in quotation marks
本文介绍了的boost ::精神::引号中的字符串输出因缘的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图逃避使用boost ::精神::因缘引号的字符串。如果它只是一个字符串,这工作得很好。然而,在一个std ::矢量一个boost ::变种一个字符串,它没有。只需打印字符串但不工作,我不明白为什么。
线(1)工作正常,但不会做我想做的。 (2)行应该这样做,但是没有。
的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&;升压/ variant.hpp>
#包括LT&;升压/精神/有/ karma.hpp>
命名空间因缘=的boost ::精神::人缘;的typedef的std ::矢量<提高::变体LT; INT,标准::字符串>>参数列表;
TYPEDEF提振::变体LT; INT,标准::字符串,参数列表>参数;主要()
{
使用因缘:: int_;
使用boost ::精神:: ASCII :: string的;
使用因缘:: EOL;
使用因缘::点燃; 产生的std ::串;
的std :: back_insert_iterator<标准::字符串>沉(产生); //(1)
因果报应::规则<的std :: back_insert_iterator<的std ::字符串>中参数列表()> parameterListRule =(int_ |字符串)%点亮(,); //这个作品! //(2)
//因缘::规则<的std :: back_insert_iterator<的std ::字符串>中参数列表()> parameterListRule =(int_ |(亮()LT;<串LT;<点亮()))%亮起(); //这不起作用 因果报应::规则<的std :: back_insert_iterator<的std ::字符串>中参数()> parameterRule =(int_ |(亮()LT;<串LT;<点亮(''))| parameterListRule)LT;< EOL; //这并不工作,尽管它也逃脱字符串中的一对引号 因果报应::生成(水槽,parameterRule,1); //作品
因果报应::生成(水槽,parameterRule,富); //作品
因果报应::生成(水槽,parameterRule,参数(参数列表{1,富})); //只有用工作规则(1)与(2)
性病::法院LT&;<生成的;
}
解决方案
如果您遍历数据类型,你应该重复你的规则。
的#include<&iostream的GT;
#包括LT&;串GT;
#包括LT&;升压/ variant.hpp>
#包括LT&;升压/精神/有/ karma.hpp>
命名空间因缘=的boost ::精神::人缘;TYPEDEF提振::变体LT; INT,标准::字符串>项目;
的typedef的std ::矢量<&项目GT;参数列表;
TYPEDEF提振::变体LT; INT,标准::字符串,参数列表>参数;诠释的main()
{
使用因缘:: int_;
使用boost ::精神:: ASCII :: string的;
使用因缘:: EOL;
使用因缘::点燃; 产生的std ::串;
的std :: back_insert_iterator<标准::字符串>沉(产生); 因果报应::规则<的std :: back_insert_iterator<的std ::字符串>中第()> itemRule =
int_ | (亮()LT;<串LT;<点亮('')); 因果报应::规则<的std :: back_insert_iterator<的std ::字符串>中参数列表()>
parameterListRule = itemRule%点亮(,); 因果报应::规则<的std :: back_insert_iterator<的std ::字符串>中参数()>
parameterRule =(int_ |(亮()LT;<串LT;<点亮(''))| parameterListRule)LT;< EOL; 因果报应::生成(水槽,parameterRule,1);
因果报应::生成(水槽,parameterRule,富);
因果报应::生成(水槽,parameterRule,参数(参数列表{1,富}));
性病::法院LT&;<生成的; 返回0;
}
I am trying to escape a string in quotation marks using boost::spirit::karma. This works fine if it's just a string. However, for a string in a boost::variant in a std::vector, it does not. Just printing the string does work however, I do not quite understand why.
Line (1) works fine, but doesn't do what I want. Line (2) should do it, but doesn't.
#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;
typedef std::vector<boost::variant<int, std::string>> ParameterList;
typedef boost::variant<int, std::string, ParameterList> Parameter;
main()
{
using karma::int_;
using boost::spirit::ascii::string;
using karma::eol;
using karma::lit;
std::string generated;
std::back_insert_iterator<std::string> sink(generated);
// (1)
karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = (int_ | string) % lit(", "); // This works!
// (2)
//karma::rule<std::back_insert_iterator<std::string>, ParameterList()> parameterListRule = (int_ | (lit('"') << string << lit('"'))) % lit(", "); // This does not work
karma::rule<std::back_insert_iterator<std::string>, Parameter()> parameterRule = (int_ | (lit('"') << string << lit('"')) | parameterListRule) << eol; // This does work, even though it also escapes the string in a pair of quotation marks
karma::generate(sink, parameterRule, 1); // Works
karma::generate(sink, parameterRule, "foo"); // Works
karma::generate(sink, parameterRule, Parameter(ParameterList{1, "foo"})); // Only works using rule (1), not with (2)
std::cout << generated;
}
解决方案
If you iterate your data types, you should iterate your rules.
#include <iostream>
#include <string>
#include <boost/variant.hpp>
#include <boost/spirit/include/karma.hpp>
namespace karma = boost::spirit::karma;
typedef boost::variant<int, std::string> Item;
typedef std::vector<Item> ParameterList;
typedef boost::variant<int, std::string, ParameterList> Parameter;
int main()
{
using karma::int_;
using boost::spirit::ascii::string;
using karma::eol;
using karma::lit;
std::string generated;
std::back_insert_iterator<std::string> sink(generated);
karma::rule<std::back_insert_iterator<std::string>, Item()> itemRule =
int_ | (lit('"') << string << lit('"'));
karma::rule<std::back_insert_iterator<std::string>, ParameterList()>
parameterListRule = itemRule % lit(", ");
karma::rule<std::back_insert_iterator<std::string>, Parameter()>
parameterRule = (int_ | (lit('"') << string << lit('"')) | parameterListRule) << eol;
karma::generate(sink, parameterRule, 1);
karma::generate(sink, parameterRule, "foo");
karma::generate(sink, parameterRule, Parameter(ParameterList {1, "foo"}));
std::cout << generated;
return 0;
}
这篇关于的boost ::精神::引号中的字符串输出因缘的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文