提升精神提取第一个单词并将其存储在向量中

发布于 2024-07-09 13:50:33 字数 470 浏览 6 评论 0原文

我在 Boost.Spirit 解析字符串时遇到问题。

该字符串看起来像

name1 has this and that.\n 
name 2 has this and that.\n 
na me has this and that.\n 

,我必须提取名称。 文本“有这个和那个”总是相同的,但名称可以包含空格,因此我不能使用 graph_p。

1)如何解析这样的字符串?

由于该字符串有几行该格式,因此我必须将名称存储在向量中。

我使用类似的方法

std::string name;
rule<> r = *graph_p[append(name)];

来保存一个名称,但是

2)在向量中保存多个名称的最佳方法是什么?

预先感谢

康拉德

I have problems with Boost.Spirit parsing a string.

The string looks like

name1 has this and that.\n 
name 2 has this and that.\n 
na me has this and that.\n 

and I have to extract the names. The text "has this and that" is always the same but the name can consist of spaces therefore I can't use graph_p.

1) How do I parse such a string?

Since the string has several lines of that format I have to store the names in a vector.

I used something like

std::string name;
rule<> r = *graph_p[append(name)];

for saving one name but

2) what's the best way to save several names in a vector?

Thanks in advance

Konrad

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

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

发布评论

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

评论(4

染年凉城似染瑾 2024-07-16 13:50:33

我认为这可以解决问题:

vector<string> names;
string name;
parse(str,
    *(  
       (*(anychar_p - "has this and that.")) [assign_a(name)]
       >> "has this and that.\n") [push_back_a(names, name)]
     ))

I think this will do the trick:

vector<string> names;
string name;
parse(str,
    *(  
       (*(anychar_p - "has this and that.")) [assign_a(name)]
       >> "has this and that.\n") [push_back_a(names, name)]
     ))
甜中书 2024-07-16 13:50:33

如果您使用较新的 Spirit V2.x(这是自 Boost V1.42 以来的默认设置),这很简单:

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

namespace qi = boost::spirit::qi;

std::vector<std::string> names;
std::string input = "name1 has this and that.\n"
                    "name 2 has this and that.\n"
                    "na me has this and that.\n";
bool result = qi::parse(
    input.begin(), input.end(),
    *(*(qi::char_ - " has this and that.\n") >> " has this and that.\n"),
    names
);

之后,如果 resulttrue,则vector names 将保存所有已解析的名称(使用 Boost V1.45 进行测试)。

If you use the newer Spirit V2.x (which is the default since Boost V1.42), this is as easy as:

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

namespace qi = boost::spirit::qi;

std::vector<std::string> names;
std::string input = "name1 has this and that.\n"
                    "name 2 has this and that.\n"
                    "na me has this and that.\n";
bool result = qi::parse(
    input.begin(), input.end(),
    *(*(qi::char_ - " has this and that.\n") >> " has this and that.\n"),
    names
);

After which, if result is true, the vector names will hold all parsed names (tested with Boost V1.45).

瞳孔里扚悲伤 2024-07-16 13:50:33

我认为您使用 Boost 是有原因的。 Spirit 而不是 STLstring查找方法? 例如:

string s = "na me has this and that.\n";
myVector . push_back( s.substr( 0, s.find( "has this and that" ) ) );

I presume there is a reason why you are using Boost.Spirit and not STL's string's find method? E.g:

string s = "na me has this and that.\n";
myVector . push_back( s.substr( 0, s.find( "has this and that" ) ) );
檐上三寸雪 2024-07-16 13:50:33

要删除“有这个和那个”,请使用:

qi::lit("has this and that")

To remove the "has this and that" use:

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