shaharhada

Reputable
Jul 27, 2020
307
6
4,685
I saw in the documentation of the c++ the description of peek:
Code:
Instream::peek (member function)
Decalration:
Int peek()

Remarks:
Returns next char with extraction.
What does this function do? (in C++)
Can somebody explain me in other words the documentation of the instruction?
 

USAFRet

Titan
Moderator

"The peek() function looks into the input stream and tells us what the next character is without removing it from the input stream. Moreover, the peek() function can store the character in a designated memory locatio without actually removing it from the stream."


Given a stream of data coming IN, the 'peek' can look at the next character, without doing anything with it.
This can be used for a prediction of what to do next.
 

shaharhada

Reputable
Jul 27, 2020
307
6
4,685

"The peek() function looks into the input stream and tells us what the next character is without removing it from the input stream. Moreover, the peek() function can store the character in a designated memory locatio without actually removing it from the stream."


Given a stream of data coming IN, the 'peek' can look at the next character, without doing anything with it.
This can be used for a prediction of what to do next.
Can you explain the underlined text?
 
Normally when you read something from a stream (which is data that's coming in from some source), you want to both read what's in the buffer (where the data is stored) and take it out of the buffer. The reason for taking out of the buffer is so you can have room for more data in case the data stream sends more stuff. As far as the program's concerned, a stream is a stream, it can be a file stream (where there's no real time constraint) or something from the internet (where you might lose the data if you don't get it out in time).

One reason to use a peek is to look ahead in the stream's buffer to determine if you need to do anything more and you don't want to copy the data somewhere. Backtracking from a stream is often not possible and having a copy of data you don't need may be wasteful.
 

shaharhada

Reputable
Jul 27, 2020
307
6
4,685
Normally when you read something from a stream (which is data that's coming in from some source), you want to both read what's in the buffer (where the data is stored) and take it out of the buffer. The reason for taking out of the buffer is so you can have room for more data in case the data stream sends more stuff. As far as the program's concerned, a stream is a stream, it can be a file stream (where there's no real time constraint) or something from the internet (where you might lose the data if you don't get it out in time).

One reason to use a peek is to look ahead in the stream's buffer to determine if you need to do anything more and you don't want to copy the data somewhere. Backtracking from a stream is often not possible and having a copy of data you don't need may be wasteful.
What is backtracking?
Backtracking from a stream is often not possible and having a copy of data you don't need may be wasteful.
 
Last edited: