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


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


basic_istream::putback()用于将字符放回输入字符串中。 iostream头文件中提供了此函数。以下是相同的语法:

头文件:

#include<iostream>

用法:

basic_istream& putback (char_type ch);

参数:

  • ch:它表示要放回输入字符串中的字符。

返回值:iostream::basic_istream::putback()返回basic_istream对象。



下面的程序可以更好地了解std::basic_istream::putback()的实现:

程序1:

// C++ code for basic_istream::putback() 
#include <bits/stdc++.h> 
  
using namespace std; 
  
int main() 
{ 
    stringstream gfg1("GeeksforGeeks"); 
    gfg1.get(); 
  
    // putback A into the input string 
    if (gfg1.putback('A')) 
        cout << gfg1.rdbuf() << endl; 
  
    istringstream gfg2("GeeksforGeeks"); 
    gfg2.get(); 
  
    if (gfg2.putback('A')) 
        cout << gfg2.rdbuf() << endl; 
    else
        cout << "putback is failed here\n"; 
  
    gfg2.clear(); 
  
    // Again putback G in the string 
    if (gfg2.putback('G')) 
        cout << gfg2.rdbuf() << endl; 
}
输出:
AeeksforGeeks
putback is failed here
GeeksforGeeks

程序2:

// C++ code for basic_istream::putback() 
#include <bits/stdc++.h> 
  
using namespace std; 
  
int main() 
{ 
    stringstream gfg1("GOOD"); 
    gfg1.get(); 
  
    // putback B into the input string 
    if (gfg1.putback('B')) 
        cout << gfg1.rdbuf() << endl; 
  
    istringstream gfg2("GOOD"); 
    gfg2.get(); 
  
    if (gfg2.putback('B')) 
        cout << gfg2.rdbuf() << endl; 
    else
        cout << "putback is failed here\n"; 
  
    gfg2.clear(); 
  
    // Again putback G in the string 
    if (gfg2.putback('G')) 
        cout << gfg2.rdbuf() << endl; 
}
输出:
BOOD
putback is failed here
GOOD

参考:http://www.cplusplus.com/reference/istream/istream/putback/




相关用法


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