gpt4 book ai didi

c++ - 使用 cin 跳过预期的字符,如 scanf()

转载 作者:塔克拉玛干 更新时间:2023-11-03 00:15:11 25 4
gpt4 key购买 nike

如何在 C++ 中用 scanf("%d # %d",&a,&b);实现类似 cin 的效果?

最佳答案

您可以通过将 # 提取到一个字符中来跳过它:

std::istringstream iss("10 # 20");

int main()
{
int a, b; char hash;
iss >> a >> hash >> b;

assert(a == 10 && b == 20);
}

关于c++ - 使用 cin 跳过预期的字符,如 scanf(),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21764826/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com