basic_istream::peek() in C++ with Examples

The std::basic_istream::peek() used to reads the next character from the input stream without extracting it. This function does not accept any parameter, simply returns the next character in the input string. Below is the syntax for the same:

Header File:

#include<iostream>

Syntax:

int peek();

Return Value: The std::basic_istream::peek() return a next character in the input string.

Below are the programs to understand the implementation of std::basic_istream::peek() in a better way:



Program 1:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for std::basic_istream::peek()
#include <bits/stdc++.h>
using namespace std;
  
// main method
int main()
{
    istringstream gfg("GeeksforGeeks");
  
    char c1 = gfg.peek();
    char c2 = gfg.get();
    char c3 = gfg.get();
  
    cout << "The first character is: "
         << c1 << endl
         << " and the next is: "
         << c3 << endl;
}

chevron_right


Output:

The first character is: G 
and the next is: e

Program 2:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for std::basic_istream::peek()
#include <bits/stdc++.h>
using namespace std;
  
// main method
int main()
{
    istringstream gfg("Computer");
  
    char c1 = gfg.peek();
    char c2 = gfg.get();
    char c3 = gfg.get();
  
    cout << "The first character is: "
         << c1 << endl
         << " and the next is: "
         << c3 << endl;
}

chevron_right


Output:

The first character is: C 
and the next is: o

Program 3:

filter_none

edit
close

play_arrow

link
brightness_4
code

// C++ code for std::basic_istream::peek()
#include <bits/stdc++.h>
using namespace std;
  
// main method
int main()
{
    istringstream gfg("Laptop");
  
    char c1 = gfg.peek();
    char c2 = gfg.get();
    char c3 = gfg.get();
    char c4 = gfg.get();
  
    cout << "The first character is: "
         << c1 << endl
         << " and the next is: "
         << c3 << endl
         << " after that next is: "
         << c4 << endl;
}

chevron_right


Output:

The first character is: L 
and the next is: a 
after that next is: p

Reference: http://www.cplusplus.com/reference/istream/basic_istream/peek/

Don’t stop now and take your learning to the next level. Learn all the important concepts of Data Structures and Algorithms with the help of the most trusted course: DSA Self Paced. Become industry ready at a student-friendly price.




My Personal Notes arrow_drop_up

Check out this Author's contributed articles.

If you like GeeksforGeeks and would like to contribute, you can also write an article using contribute.geeksforgeeks.org or mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.

Please Improve this article if you find anything incorrect by clicking on the "Improve Article" button below.


Article Tags :
Practice Tags :


Be the First to upvote.


Please write to us at contribute@geeksforgeeks.org to report any issue with the above content.