📜  C++中的basic_istream :: unget()示例

📅  最后修改于: 2021-05-30 18:12:38             🧑  作者: Mango

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



句法:

basic_istream& unget();

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

CPP14
// C++ code for basic_istream::unget()
#include 
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;
}


CPP14
// C++ code for basic_istream::unget()
#include 
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: G
After ungetting the character once again we got: G

程式2:

CPP14

// C++ code for basic_istream::unget()
#include 
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/

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