我想读一个简单的2D int数组(空格分隔)为here:
qi::phrase_parse(b, e, +qi::int_ % qi::eol, qi::space - qi::eol, vectors)
但有两点不同:
是否可以将其作为一个衬垫,例如没有编写我自己的解析器?就像在the link mentioned above中一样简单?
答案 0 :(得分:3)
假设你的意思是精神版本("作为一个班轮"),下面是一个改编版本,增加了元素数量的检查。
如果你想要更多的控制(并且直接进行输入检查,而不是后见之明')那么我建议你看看我写的另一个答案,其中显示了三种方法:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/spirit/include/karma.hpp>
namespace spirit = boost::spirit;
namespace qi = boost::spirit::qi;
namespace karma = boost::spirit::karma;
int main()
{
std::cin.unsetf(std::ios::skipws);
spirit::istream_iterator b(std::cin), e;
std::vector<std::vector<int> > vectors;
if (qi::phrase_parse(b, e, +qi::int_ % qi::eol >> qi::eoi, qi::blank, vectors))
{
std::cerr << "Parse failed at '" << std::string(b,e) << "'\n";
return 255;
}
// check all rows have equal amount of elements:
const auto number_of_elements = vectors.front().size();
for (auto& v : vectors)
if (v.size() != number_of_elements)
std::cerr << "Unexpected number of elements: " << v.size() << " (expected: " << number_of_elements << ")\n";
// print the data for verification
std::cout
<< karma::format(karma::right_align(8)[karma::auto_] % ',' % '\n', vectors)
<< std::endl;
return 0;
}
业力位不是必需的(他们只是在那里输出整个事物进行演示)。
要构建更积极的错误检查,您可以执行以下操作:
int num_elements = 0;
bool ok = qi::phrase_parse(b, e,
(+qi::int_) [ phx::ref(num_elements) = phx::size(qi::_1) ]
>> *(qi::eol >> qi::repeat(phx::ref(num_elements)) [ qi::int_ ])
>> *qi::eol,
qi::blank, vectors);
使用qi::repeat
期望后续行中num_elements
个元素的数量。你可以将它存储到一维数组中:
#include <boost/spirit/include/qi.hpp>
#include <boost/spirit/include/phoenix.hpp>
#include <boost/spirit/include/support_istream_iterator.hpp>
#include <boost/spirit/include/karma.hpp>
namespace qi = boost::spirit::qi;
namespace phx = boost::phoenix;
namespace karma = boost::spirit::karma;
int main()
{
std::cin.unsetf(std::ios::skipws);
boost::spirit::istream_iterator b(std::cin), e;
//std::vector<std::vector<int> > vectors;
std::vector<int> vectors;
int num_elements = 0;
bool ok = qi::phrase_parse(b, e,
(+qi::int_) [ phx::ref(num_elements) = phx::size(qi::_1) ]
>> *(qi::eol >> qi::repeat(phx::ref(num_elements)) [ qi::int_ ])
>> *qi::eol,
qi::blank, vectors);
std::cout << "Detected num_elements: " << num_elements << "\n";
if (!ok)
{
std::cerr << "Parse failed at '" << std::string(b,e) << "'\n";
return 255;
}
if (b!=e)
std::cout << "Trailing unparsed: '" << std::string(b,e) << "'\n";
// print the data for verification
std::cout
<< karma::format_delimited(karma::columns(num_elements)[+karma::int_], ' ', vectors)
<< std::endl;
return 0;
}
请注意使用karma::columns(num_elements)
将输出拆分为每行正确的列数。