Refers to a non-destructive operation performed on sequential, collection-like data structures that have the notion of "top element" or "next element", such as stacks, queues and streams. The `peek` operation returns the value of the "top" (or "next") element without "consuming" that element, i.e. without removing that element from the data structure.
Questions tagged [peek]
163 questions
13
votes
1 answer
C++ fstream function that reads a line without extracting?
In C++, is there a function in the fstream library (or any library) that allows me to read a line to a delimiter of '\n' without extracting?
I know the peek() function allows the program to 'peek' at the next character its reading in without…

SexyBeastFarEast
- 143
- 1
- 1
- 7
11
votes
1 answer
How to correctly use `peek()` in Rust?
I am trying to do something simple. In a slice of u8, I want to find occurrence of two characters "\r\n". However, I cannot convert that slice into String using from_utf8 because parts of slice after "\r\n" may not be utf-8 and as far as possible I…

gabhijit
- 3,345
- 2
- 23
- 36
10
votes
1 answer
Python: why does peek(1) return 8K bytes instead of 1 byte?
I'm using Python 3, and the peek() method for buffered file I/O doesn't seem to work as documented. For example, the following code illustrates the problem -- it prints 8192 as the length of the byte string returned by f.peek(1):
jpg_file =…

Doug Mahugh
- 624
- 6
- 15
9
votes
2 answers
Help: ZX81 'BASIC' Peek function
I need a way to find if the character ('<') has hit a wall (Black pixel Graphic)
-On a ZX81 game.
I'm been looking at another game... which uses code
if peek(peek 16398 +256*peek 16399) = code "blackpixel graphic" then ...
Which seems to work for…

James Andrew
- 7,197
- 14
- 46
- 62
9
votes
6 answers
Why is Stack.Peek() a method?
As in the title. Why does the Stack class need a method to return a reference of the top object? I've always been told, that methods suggest there's some computing involved and that simple objects should be returned with properties instead. Peek()…

Tarec
- 3,268
- 4
- 30
- 47
9
votes
3 answers
Is it possible to Peek below the surface of a Stack?
Is it possible to Peek at the value of a C# Stack variable layers below the surface? In my example, I need to Peek at the value one below. I could Peek to record the value into a temporary variable, Pop and read, then Push the temporary values back,…

Keavon
- 6,837
- 9
- 51
- 79
8
votes
1 answer
How to properly use cin.peek()
This function is supposed to read a fraction and place it in an array. If the user enters '0' the function is supposed to exit. I am trying to do this using the cin.peek() function but execution always goes into the if statement and doesn't allow…

Zzz
- 2,927
- 5
- 36
- 58
8
votes
7 answers
pop and peek doing same in that case?
From all the sources I've read, they say - the difference between peek and pop is that peek doesn't remove the top value.
In the provided example from my lecture notes, apparently they do the same using a different method of subtraction. After both…

Peter Cerba
- 806
- 4
- 14
- 26
7
votes
2 answers
peek() Multiple Places Ahead?
Lets say I have an outer while loop to read each character and output it to console. I also want to flag a word if it is found and by using the peek method I can find the first instance of a word. Is there a way to peek multiple places ahead. For…

domonica
- 526
- 7
- 14
7
votes
1 answer
python2.7 peek at stdin
I would like to call sys.stdin.readlines() without removing anything from stdin. I am using Python2.7 on Linux.
For example, what I want is:
x = sys.stdin.readlines()
y = sys.stdin.readlines()
then x and y have identical strings. It would be…

Aaron
- 2,344
- 3
- 26
- 32
7
votes
3 answers
Groovy 'Peeking' ahead with an iterator?
I have various scenarios in loops where I would 'peek' or 'skip' ahead while iterating through items for processing.
One scenario is I'm enumerating through lines of a file, and there is a 'continuation' character at the end of a line indicating…

MarkE
- 123
- 8
6
votes
2 answers
How to peek on channels without blocking and still being able to detect hangup?
According to the doc of .try_iter() method of the Receiver end of a Rust std::mpsc::channel, I understand that this iterator either yield "None":
when there is no data in the channel
or when the other end of the channel has hung up.
In my case, I…

iago-lito
- 3,098
- 3
- 29
- 54
6
votes
3 answers
Peeking at a UDP message in c++
I'm trying to receive a UDP message using sockets in c++.
I'm sending the size of the message in the header, so I can know how much memory should I allocate, so I try to peek at the beggining of the message like this:
int bytesRead =…

Idov
- 5,006
- 17
- 69
- 106
6
votes
2 answers
Does JMS have a concept of a queue peek?
From a general Computer Science perspective - when we think of a Queue in a logical sense - we think of being able to 'peek' the first item in the queue.
When I look at the JMS API - it has a MessageListener - which has an OnMessage() method. This…

hawkeye
- 34,745
- 30
- 150
- 304
5
votes
5 answers
C#: Using StreamReader to read line from txt file, but Peek() return -1 even there are a lot of lines left
I use Peek() method of StreamReader to check whether there are more lines need to be processed. There are more than 1000 lines in my file, but Peek() suddenly return -1 when it reachs line#750. I checked but seems no differences between line#750 and…

Alan
- 93
- 1
- 2
- 5