cuốn sách gpt4 ai đã làm

C++ getline() 分隔符

In lại Tác giả: Walker 123 更新时间:2023-11-28 03:08:43 27 4
mua khóa gpt4 Nike

嘿,我正在尝试使用 getline 读取以下行

(15,0,1,#)

(2,11,2,.)

(3,20,0,S)

我希望能够将整数提取为 int,将字符提取为 char,但我不知道如何只提取它们。

câu trả lời hay nhất

您似乎可以读出分隔符,即 '(', ')'',' 然后就可以了使用格式化输入。为操纵器使用一个简单的模板应该可以很好地解决问题:

#include 
#include

template
std::istream& read_char(std::istream& in)
{
if ((in >> std::ws).peek() == C) {
in.ignore();
}
khác {
in.setstate(std::ios_base::failbit);
}
trở lại ;
}

auto const open_paren = &read_char<'('>;
auto const close_paren = &read_char<')'>;
auto const comma = &read_char<','>;

int chính()
{
int x, y, z;
ký tự 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';
}
}

关于C++ getline() 分隔符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19035837/

27 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress