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


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


basic_istream::unget()用于取出字符,并将位置减少一个字符,并使提取的字符可再次使用。头文件:

<iostream>

用法:

basic_istream& unget();

参数:basic_istream::unget()方法不接受任何参数。返回值:函数basic_istream::unget()返回basic_istream对象。下面是说明std::basic_istream::unget():Program 1的程序:

CPP14

// C++ code for basic_istream::unget() 
#include <bits/stdc++.h> 
using namespace std; 
  
// Driver code 
int main() 
{ 
    // Declare string stream 
    istringstream gfg("GeeksforGeeks"); 
  
    char a = gfg.get(); 
    if (gfg.unget()) { 
        char b = gfg.get(); 
        cout << "We got:" << a << endl; 
        cout << "After ungetting the "
             << "character once again"
             << " we got:"
             << b << endl; 
    } 
  
    return 0; 
}
输出:
We got:G
After ungetting the character once again we got:G

程序2:



CPP14

// C++ code for basic_istream::unget() 
#include <bits/stdc++.h> 
using namespace std; 
  
// Driver code 
int main() 
{ 
    // Declare string stream 
    istringstream gfg("Laptop"); 
  
    char a = gfg.get(); 
    if (gfg.unget()) { 
        char b = gfg.get(); 
        cout << "We got:" << a << endl; 
        cout << "After ungetting the "
             << "character once again"
             << " we got:"
             << b << endl; 
    } 
  
    return 0; 
}
输出:
We got:L
After ungetting the character once again we got:L

参考:http://www.cplusplus.com/reference/istream/basic_istream/unget/




相关用法


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