basic_istream::peek() in C++ with Examples Last Updated : 28 May, 2020 Comments Improve Suggest changes Like Article Like Report 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: CPP14 // 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; } Output: The first character is: G and the next is: e Program 2: CPP14 // 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; } Output: The first character is: C and the next is: o Program 3: CPP14 // 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; } 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/ Comment More infoAdvertise with us Next Article basic_istream::peek() in C++ with Examples B bansal_rtk_ Follow Improve Article Tags : Misc C++ CPP-Functions Practice Tags : CPPMisc Similar Reads basic_istream::seekg() in C++ with Examples The basic_stream::seekg() method is used to set position of the next character to be extracted from the input stream. This function is present in the iostream header file. Below is the syntax for the same: Header File: #include<iostream> Syntax: basic_istream& seekg (pos_type pos); Paramet 2 min read basic_istream::swap() in C++ with Examples The basic_ios::swap(x) is used swap all data members of the base class except for rdbuf(), and swaps the values of the gcount() counters between *this and x. This basic_ios::swap(x) function is a protected function. Below is the syntax and the header file for the same: Header File: #include<iostr 2 min read 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 accept 2 min read basic_istream::putback() in C++ with Examples The basic_istream::putback() used to put the character back in the input string. This function is present in the iostream header file. Below is the syntax for the same: Header File: #include<iostream> Syntax: basic_istream& putback (char_type ch); Parameter: ch: It represents the character to 2 min read basic_istream::readsome() in C++ with Examples The basic_istream::readsome() used to read the data from the buffer and extracts up to n immediately available characters from the input string. This function returns the number of extracted characters. Below is the syntax for the same: Header File: #include<iostream> Syntax: streamsize readso 2 min read Like