当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


C++ basic_istream::get()用法及代码示例


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

头文件:

<iostream>

用法:

int_type get();

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

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



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

程序1:

// C++ code for basic_istream::get() 
#include <bits/stdc++.h> 
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 <bits/stdc++.h> 
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




相关用法


注:本文由纯净天空筛选整理自bansal_rtk_大神的英文原创作品 basic_istream::get() in C++ with Examples。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。