跳过期望的字符,如scanf()with cin [英] Skipping expected characters like scanf() with cin
本文介绍了跳过期望的字符,如scanf()with cin的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何实现 scanf(%d#%d,& a,& b);
/ code>
How to achieve scanf("%d # %d",&a,&b);
sort of effect with cin
in C++ ?
推荐答案
您可以创建自己的流操纵器。这很容易。
You could create your own stream manipulator. It is fairly easy.
#include <ios>
#include <iostream>
using namespace std;
struct skip
{
const char * text;
skip(const char * text) : text(text) {}
};
std::istream & operator >> (std::istream & stream, const skip & x)
{
ios_base::fmtflags f = stream.flags();
stream >> noskipws;
char c;
const char * text = x.text;
while (stream && *text++)
stream >> c;
stream.flags(f);
return stream;
}
int main()
{
int a, b;
cin >> a >> skip(" # ") >> b;
cout << a << ", " << b << endl;
return 0;
}
这篇关于跳过期望的字符,如scanf()with cin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文