basic_istream::unget() in C++ with Examples
The basic_istream::unget() is used to unget the character and used to decrease the location by one character and makes the extracted character available for used once again.
Header File:
<iostream>
Syntax:
basic_istream& unget();
Parameters: The basic_istream::unget() method doesn’t accepts any parameter.
Return Value: The function basic_istream::unget() returns the basic_istream object.
Below is the programs to illustrate 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( "w3wiki" ); 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; } |
Output:
We got: G After ungetting the character once again we got: G
Program 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; } |
Output:
We got: L After ungetting the character once again we got: L
Reference: http://www.cplusplus.com/reference/istream/basic_istream/unget/
Contact Us