如何在std :: getline中自定义分隔符

问题描述 投票:0回答:1

嘿,我在hackerRank上解析属性解析器,但是我卡在定界符上,我想从(任何东西)中抓取一个元素,在这种情况下,它将给我(任何东西),但是当我这样做时

while (getline(ob, item, '""')) {//but its be true when i put single one ('"')
        std::cout << item << "\n";
    }

它给了我这个错误:严重级别代码说明项目文件行抑制状态错误(活动)E0304没有“ getline”的实例与参数列表匹配

c++ string arguments delimiter istringstream
1个回答
0
投票

正如@RetiredNinja所说,您可以使用'\"'作为分隔符。代码可能看起来像这样:

#include <iostream>
#include <string>


int main()
{
    std::string str;
    while (std::getline(std::cin, str, '\"'))
        if (!str.empty() && str[0] != '(' && str[0] != ')')
            std::cout << '(' << str << ")\n";
}

您可以根据问题的规则更改代码。请注意,定界符将首先给您"(",然后是实际单词,依此类推。因此,您必须过滤不感兴趣的值。

© www.soinside.com 2019 - 2024. All rights reserved.