本篇内容介绍了“istream常用函数有哪些”的有关知识,在实际案例的操作过程中,不少人都会遇到这样的困境,接下来就让小编带领大家学习一下如何处理这些情况吧!希望大家仔细阅读,能够学有所成!
标准库
get
获取单个字符
int get();
istream& get (char& c);
从输入缓冲区获取单个字符,无参数时返回获取到的字符的asc码,有参数时把获取到的字符存储到参数中。
代码测试:
#include <iostream>
using namespace std;
int main () {
char a, b, c;
int res = cin.get();
cout << "res: " << res << endl;
cin.get(a).get(b).get(c);
cout << "a: " << a << "\nb: " << b << "\nc: " << c << endl;
return 0;
}
输入:abcdef
输出:
res: 97
a: b
b: c
c: d2.获取c风格的字符串
istream& get (char* s, streamsize n);
istream& get (char* s, streamsize n, char delim);
代码测试:
#include <iostream>
using namespace std;
int main () {
char ch[1024];
cin.get(ch, 10);
cout << "ch2: " << ch << endl;
cin.get(ch, 10, ',');
cout << "ch3: " << ch << endl;
cin.get(ch, 10, ',');
cout << "ch4: " << ch << endl;
return 0;
}
输入:
aaabbbcccddd,eeefffhhhiii
输出:
ch2: aaabbbccc
ch3: ddd
ch4:3.获取streambuf
istream& get (streambuf& sb);
istream& get (streambuf& sb, char delim);
分隔符delim不设置时,默认为\n。
结束条件:下一个要获取的为分隔符。
#include <iostream>
#include <sstream>
using namespace std;
int main () {
stringbuf sb;
cin.get(sb, ',');
cout << sb.str();
return 0;
}
输入:
abc
def,
输出:
abc
defgetline
ignore
peek
putback
“istream常用函数有哪些”的内容就介绍到这里了,感谢大家的阅读。如果想了解更多行业相关的知识可以关注天达云网站,小编将为大家输出更多高质量的实用文章!