📜  C++中的basic_istream :: get()与示例

📅  最后修改于: 2021-05-30 02:29:15             🧑  作者: Mango

basic_istream :: get()用于获取字符。如果可用,此函数将返回一个字符,否则将返回文件末尾。

头文件:


句法:

int_type get();

参数:方法basic_istream :: get()不接受任何参数。

返回值:方法basic_istream :: get()返回一个字符(如果可用),否则返回文件末尾。

下面是说明basic_istream :: get()的程序

程序1:

// C++ code for basic_istream::get()
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Declare string stream
    istringstream gfg("GeeksforGeeks");
  
    // Here we get H
    char a = gfg.get();
    cout << "First character is: "
         << a << endl;
  
    char b;
    // Here we got e
    gfg.get(b);
    cout << "After reading:" << a
         << " We got " << b << endl;
  
    return 0;
}
输出:
First character is: G
After reading:G We got e

程式2:

// C++ code for basic_istream::get()
#include 
using namespace std;
  
// Driver Code
int main()
{
    // Declare string stream
    istringstream gfg("Computer");
  
    // Here we get C
    char a = gfg.get();
    cout << "First character is: "
         << a << endl;
  
    char b;
    // Here we got o
    gfg.get(b);
    cout << "After reading:" << a
         << " We got " << b << endl;
  
    char c;
    // Here we got m
    gfg.get(c);
    cout << "Now we got : "
         << c << endl;
  
    return 0;
}
输出:
First character is: C
After reading:C We got o
Now we got : m

参考: std :: basic_istream :: get

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”