Basic Istream Unget In C++ - C++ Programming Tutorial
C++ Course / File Handling / Basic Istream Unget In C++

Basic Istream Unget In C++

BLUF: Mastering Basic Istream Unget In C++ is a critical step in becoming a proficient C++ developer. This lesson provides a deep dive into the syntax, performance considerations, and real-world applications of this concept.
Key Performance Insight: Basic Istream Unget In C++

C++ is renowned for its efficiency. Learn how Basic Istream Unget In C++ enables low-level control and high-performance computing in the tutorial below.

The basic_istream::unget method is employed to push back a character, decrementing the position by one character and enabling the character to be used again. Make sure to include the correct header file for this operation.

The purpose of employing the basic_istream::unget function is to send a character back to the stream, enabling its convenient handling at a subsequent point. This method shifts the position back by one character, effectively reinstating the character that was previously taken out.

Header File

It included the <iostream> header file.

Syntax:

It has the following syntax:

Example

basic_istream& unget();

The basic_istream::unget function does not accept any parameters.

The method basicistream::unget returns the basicistream object as its return value.

The following programs explain std::basic_istream::unget:

Example:

Filename: Ungetting1.cpp

Example

// Program to implement the use of basic_istream::unget() 

#include <bits/stdc++.h> 

using namespace std; 

// main

int main() 

{ 

	// decreasing the size of the string stream

	istringstream con("ProgrammingLanguage"); 

	char a = con.get(); 

	if (con.unget()) { 

		char be = con.get(); 

		cout << "We got: " << a << endl; 

		cout << "After the ungetting, the character is: "

			<< be << endl; 

	} 

	return 0; 

}

Output:

Output

We got: P

After the ungetting, the character is: P

Example 2:

Filename: Ungetting2.cpp

Example

// Program to implement the use of basic_istream::unget() 

#include <bits/stdc++.h> 

using namespace std; 

// main

int main() 

{ 

	// decreasing the size of the string stream

	istringstream con("Language"); 

	char a = con.get(); 

	if (con.unget()) { 

		char be = con.get(); 

		cout << "We got: " << a << endl; 

		cout << "After the ungetting, the character is: "

			<< be << endl; 

	} 

	return 0; 

}

Output:

Output

We got: L

After the ungetting, the character is: L

Explanation:

  • An istringstream object con is created with the string "Language" to set up an input string stream.
  • A character variable a is assigned the value retrieved by calling con.get. If con.unget is true, the first character 'L' from the stream is read and stored in a. { ... }: This section demonstrates the process of moving back one character in the stream using unget. The unget function returns true if it successfully moves back the stream position by one character.

Within the if statement:

When

  • char be = con.get; is executed after unget, it retrieves an element from the stream. It is expected to output the same element ('L') that was previously retrieved and then "unget" back into the stream.

Following this, the values of variables a and be are displayed on the terminal.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience