如何在 std::getline 中自定义分隔符
how to customize the delimiter in std::getline
我正在解决hackerRank上的属性解析器,但我被定界符困住了。我想从("任何事物"(中获取一个元素,在这种情况下,它会给我(任何事物(,但是当我这样做时:
while (getline(ob, item, '""')) {//but its be true when i put single one ('"')
std::cout << item << "n";
}
它给了我这个错误:
E0304 没有"getline"实例与参数列表匹配
正如@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";
}
您可以根据问题的规则更改代码。请注意,分隔符会首先为您提供"("
,然后是实际单词,依此类推。因此,您必须过滤您不感兴趣的值。
相关文章:
- C++映射:具有自定义类的运算符[]不起作用(总是返回0)
- 如何将点击的信号和插槽添加到qt中的自定义按钮中
- C++自定义比较函数
- 如何比较自定义类的std::变体
- std::设置自定义比较器
- 如何正确实现和访问运算符的各种自定义枚举器
- flutter:即使shouldRepaint()返回true,自定义画家也不会重新绘制
- 自定义先决条件对移动分配运算符有效吗
- 使用VS Code和CMake Tools运行自定义命令
- 如何创建从Maya(或类似程序)到虚幻引擎的自定义数据导出插件
- std::ranges::elements_view,用于自定义类似元组的数据
- 跟随整数索引列表的自定义类迭代器
- 参数化自定义CMake工具链
- 使用自定义比较函数使用std::sort()对矢量字符串进行排序时出现问题
- 如何在自定义类中启用'auto loops'?
- 使用QJsEngine在Qt中注册自定义类型
- 如何在 std::getline 中自定义分隔符
- 通过自定义分隔符从文件中读取对象数组
- std::矢量到带有自定义分隔符的字符串
- 如何实现自动添加分隔符的自定义粘性操纵器