C++ getline() delimitter
C++ getline() delimitter
嘿,我正在尝试使用 getline 阅读以下行
(15,0,1,#)
(2,11,2,.)
(3,20,0,秒)
我希望能够将整数提取为整数,将字符提取为字符,但我不知道如何仅提取这些整数。
似乎您可以读取分隔符,即 '('
、 ')'
和 ','
,然后只使用格式化的输入。为操纵器使用一个简单的模板应该可以很好地解决问题:
#include <iostream>
#include <sstream>
template <char C>
std::istream& read_char(std::istream& in)
{
if ((in >> std::ws).peek() == C) {
in.ignore();
}
else {
in.setstate(std::ios_base::failbit);
}
return in;
}
auto const open_paren = &read_char<'('>;
auto const close_paren = &read_char<')'>;
auto const comma = &read_char<','>;
int main()
{
int x, y, z;
char c;
std::istringstream in("(1, 2, 3, x)n(4, 5, 6, .)");
if (in >> open_paren >> x
>> comma >> y
>> comma >> z
>> comma >> c
>> close_paren) {
std::cout << "x=" << x << " y=" << y << " z=" << z << " c=" << c << 'n';
}
}
比较从 getline() 的十六进制值获得的值,并运行一些if
语句与 ASCII 进行比较。 这会告诉你你是否抓住了一个数字、字母或符号。
相关文章:
- Seg Fault Issue C++ (file IO / getline)
- 当用户键入分隔符时,停止getline()输入
- getline() 的原型/库是什么;
- 错误 没有与参数列表匹配的重载函数"getline"实例
- 如何在 c++ 中使用 ',' 作为 getline 分隔符
- 错误:调用'getline'没有匹配函数
- 如何在 c++ 中使用 getline 从文件中读取字符串?
- 将 getline() 与文件一起使用
- 我有三个 getline,但是一旦编译,输入就太多了
- 在 std::getline 和 std::cin 期间卡在循环中
- 为什么 getline() 不读取文本文件中的所有内容?
- 有没有办法在不使用 getline() 的情况下从.csv文件中读取?
- 确切地说,如何解释 std::getline(stream, string) 函数在C++中填充的字符串
- std::getline没有在while循环中重新请求用户输入
- 谁能告诉我我用 getline 做错了什么 (cpp) 格式
- Getline(cin,str)的奇怪输出
- 我在C++中使用 getline() 函数时遇到问题
- 而循环跳过 cin.getline() 对于 C 字符串
- 如何在 getline 中删除/忽略超过特定数字的字符?
- C++ getline() delimitter