std::basic_istream::get
int_type get(); | (1) | |
|---|---|---|
basic_istream& get( char_type& ch ); | (2) | |
basic_istream& get( char_type* s, std::streamsize count ); | (3) | |
basic_istream& get( char_type* s, std::streamsize count, char_type delim ); | (4) | |
basic_istream& get( basic_streambuf& strbuf ); | (5) | |
basic_istream& get( basic_streambuf& strbuf, char_type delim ); | (6) | |
从流中提取字符。
所有版本都表现为UnformattedInputFunction斯.在构造和检查哨兵对象之后,这些函数执行以下操作:
1%29读取一个字符并在可用时返回它。否则,返回Traits::eof()和集failbit和eofbit...
2%29读取一个字符并将其存储到ch如果有的话。否则,树叶ch未修改集failbit和eofbit请注意,此函数在类型上没有重载。signed char和unsigned char,与格式化的字符输入操作符>>不同。
3%29get(s, count, widen('\n')),也就是说,最多读一读。count-1将它们存储到字符串中。s直到'\n'被发现了。
4%29读取字符并将它们存储到字符数组的连续位置,该字符数组的第一个元素由s提取和存储字符,直到出现下列任何情况:
- n-1字符已被存储。
- 文件结束条件发生在输入序列%28中。
setstate(eofbit)称为%29
- 下一个可用输入字符
c等号delim,由Traits::eq(c, delim)此字符不提取%28不像basic_istream::getline()%29
如果没有提取字符,则调用setstate(failbit)在任何情况下,如果count>0,空字符%28CharT()存储在数组的下一个连续位置中。
5%29get(strbuf, widen('\n')),即读取可用字符并将它们插入给定的basic_streambuf对象直到'\n'被发现了。
6%29读取字符并将它们插入到由给定的输出序列控制的输出序列中。basic_streambuf对象。提取字符并插入strbuf直到发生下列任何一种情况:
- 文件结束条件发生在输入序列中。
- 插入输出序列失败%28,在这种情况下,无法插入的字符不能提取%29
- 下一个可用输入字符
c等号delim,由Traits::eq(c, delim)这个字符是不提取的。
- 异常发生在%28,在这种情况下,异常会被捕获,而不会重新引发%29。
如果没有提取字符,则调用setstate(failbit)...
所有版本都会设置gcount()提取的字符数。
参数
ch | - | reference to the character to write the result to |
|---|---|---|
s | - | pointer to the character string to store the characters to |
count | - | size of character string pointed to by s |
delim | - | delimiting character to stop the extraction at. It is not extracted and not stored. |
strbuf | - | stream buffer to read the content to |
返回值
1%29提取字符或Traits::eof()
2-6%29*this
例外
failure如果发生错误%28,则错误状态标志不是goodbit29%和exceptions()将被抛向那个州。
如果内部操作抛出异常,则会捕获该操作,并且badbit已经设定好了。如果exceptions()设置为badbit,异常将被重新抛出。
例
二次
#include <sstream>
#include <iostream>
int main()
{
std::istringstream s1("Hello, world.");
char c1 = s1.get(); // reads 'H'
std::cout << "after reading " << c1 << ", gcount() == " << s1.gcount() << '\n';
char c2;
s1.get(c2); // reads 'e'
char str[5];
s1.get(str, 5); // reads "llo,"
std::cout << "after reading " << str << ", gcount() == " << s1.gcount() << '\n';
std::cout << c1 << c2 << str;
s1.get(*std::cout.rdbuf()); // reads the rest, not including '\n'
std::cout << "\nAfter the last get(), gcount() == " << s1.gcount() << '\n';
}二次
产出:
二次
after reading H, gcount() == 1
after reading llo,, gcount() == 4
Hello, world.
After the last get(), gcount() == 7二次
另见
read | extracts blocks of characters (public member function) |
|---|---|
operator>> | extracts formatted data (public member function) |
operator>>(std::basic_istream) | extracts characters and character arrays (function template) |
© cppreference.com在CreativeCommonsAttribution下授权-ShareAlike未移植许可v3.0。
本文档系腾讯云开发者社区成员共同维护,如有问题请联系 cloudcommunity@tencent.com

