gpt4 book ai didi

c++ - 在精神规则中定义函数给出警告

转载 作者:搜寻专家 更新时间:2023-10-31 02:21:54 24 4
gpt4 key购买 nike

qi::repeat(1,2) 规则中的功能给我警告,我不想忽略该警告,所以我想优化此代码,例如分离规则 来自解析方法。

qi::phrase_parse 正在做与 rule 相同的事情,但我想分离规则并将规则赋予 pharse_parse 函数。

std::ifstream ifs("f:/test.txt");
std::string line;

//In header in my code
std::vector<unsigned long long> v_BF_Char;


//qi::int_parser<uintmax_t, 16> hex_int;
static qi::uint_parser<unsigned long long, 16, 16, 16> hex_int;

while (std::getline(ifs, line))
{
typedef std::string::const_iterator It;
It begin = line.begin(), end = line.end();

// rule for grammer
qi::rule<It, unsigned long long()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';


bool ok = qi::phrase_parse(begin, end,
*('<' >> qi::repeat(1,2)[ hex_int ] >> '>'), qi::space, v_BF_Char);

想做这样的事情

bool ok = qi::phrase_parse(begin, end,
braced_hex , qi::space, v_BF_Char);

test.txt包含

<51dc> <04001C0180000000000000000EE317BC>
<05001C0180000000> <04001C0180000000000000000EE317BC>
<51dc> <30ea30f330ae30c330c8>
<0000> <fffd>
<003d> <00a5>
<005d> <00a6>
<005e> <007d>
<005f> <0303>
<0060> <2019>
<0061> <005c>
<0062> <2018>
<0063> <007c>
<0064> <007e>
<0068> <2044>
<0069> <0192>
<006a> <00a7>
<006b> <00a4>
<006c> <201c>

我的代码仅适用于第二行并提取 vector 中的值,但不适用于其他行值。

最佳答案

修复规则的暴露属性:

qi::rule<It, std::vector<unsigned long long>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

重复 exposes a vector of hex_int's exposed attribute type

现场演示:

Live On Coliru

#include <boost/spirit/include/qi.hpp>
#include <fstream>

namespace qi = boost::spirit::qi;

int main() {
std::ifstream ifs("input.txt");

//In header in my code
std::vector<unsigned long long> v_BF_Char;

//qi::int_parser<uintmax_t, 16> hex_int;
static qi::uint_parser<uint64_t, 16, 16, 16> hex_int;

std::string line;
while (std::getline(ifs, line))
{
typedef std::string::const_iterator It;
It begin = line.begin(), end = line.end();

// rule for grammer
qi::rule<It, std::vector<uint64_t>()> braced_hex = '<' >> qi::repeat(1,2)[hex_int] >> '>';

bool ok = qi::phrase_parse(begin, end, *braced_hex, qi::space, v_BF_Char);
assert(ok);
}

std::cout << "Parsed: " << v_BF_Char.size() << " 64bit elements\n";
}

打印:

Parsed: 50 64bit elements

例如对于这个输入:

<1353cd278dd1f003636bc155006ac5ce> <1e83b053032565f0> <d1e97c841e68153a5d82d57df3074a21>
<92adee538fd147a337ebc8a4fc8d0ad3> <0ed9fb22ab42b3a4> <756ad64486054d22c62329e8dcaef0c5>
<16eeaec1108b1159b49c6bf884564519> <b4b87d1fd1aa10af> <1f710495fd863a1d191355adf1b33d5a>
<947ac523b4450ec26446840ccde3965b> <faa860f7763b23dc> <571decbfd0fcfe9a4047f72c101b9d87>
<50d726028b79b1a531a2c3752a4fdde7> <644e057721fa7fe1> <6bf66d2e1ae50351db53eddcee5fae41>
<6916580258e94f2be66eb71f103d3023> <a427df9bd05edd6d> <d896cfe92e6634867fcab5c6fc2de60b>
<9e50d5c9cda9e9a2fbf78eeb10f3a6bd> <9cff72edea319328> <0aabc7f36fcd058a2dfa7bb94602919a>
<923832f107c94d4a04b1de96241fda14> <003c7554390cabaf> <c43d58504fc6659bb226707efc0221b8>
<1040a8d23eac10e9e4b6abb2efcde1bd> <f38ac3906542529d> <ffbd836c54b0f498d358e4ea50170c94>
<7c4c6fd86a60cf7b4ac62faa0395c06b> <61156478683a6b01> <fb92ef7030068f25471e8049fb0f7cd3>

random data generated with

od -A none -t x8 -w256 < /dev/urandom | head | awk '{ print "<" $1 $2 "> <" $3 "> <" $4 $5 ">" }'

关于c++ - 在精神规则中定义函数给出警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30883431/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com