📜  C++中的basic_istream :: putback()及其示例

📅  最后修改于: 2021-05-30 17:03:08             🧑  作者: Mango

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

头文件:

#include

句法:

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 
  
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 
  
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/

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