peek

Is there a “HasNext” method for an IEnumerator?

半腔热情 提交于 2019-11-30 13:59:01
问题 With Java Iterator s, I have used the hasNext method to determine whether an iteration has more elements (without consuming an element) -- thus, hasNext is like a " Peek " method. My question: is there anything like a " hasNext " or " Peek " method with C#'s generic IEnumerator s? 回答1: No, unfortunately there isn't. The IEnumerator<T> interface only exposes the following members: Methods: Dispose MoveNext Reset Properties : Current 回答2: No, but in C# you can repeatedly ask for the current

Can I peek on a BufferedReader?

喜你入骨 提交于 2019-11-30 11:11:58
Is there a way to check if in BufferedReader object is something to read? Something like C++ cin.peek() . Thanks. You can try the "boolean ready()" method. From the Java 6 API doc: "A buffered character stream is ready if the buffer is not empty, or if the underlying character stream is ready." BufferedReader r = new BufferedReader(reader); if(r.ready()) { r.read(); } Gavin H You can use a PushbackReader . Using that you can read a character, then unread it. This essentially allows you to push it back. PushbackReader pr = new PushbackReader(reader); char c = (char)pr.read(); // do something to

java Queue 的remove/poll, add/offer, element/peek

冷暖自知 提交于 2019-11-30 09:47:31
原文链接: https://blog.csdn.net/liuyongvs2009/article/details/42454779 这里简单对其重复的方法做点简单的区分。 offer,add区别: 一些队列有大小限制,因此如果想在一个满的队列中加入一个新项,多出的项就会被拒绝。 这时新的 offer 方法就可以起作用了。它不是对调用 add() 方法抛出一个 unchecked 异常,而只是得到由 offer() 返回的 false。 poll,remove区别: remove() 和 poll() 方法都是从队列中删除第一个元素。remove() 的行为与 Collection 接口的版本相似, 但是新的 poll() 方法在用空集合调用时不是抛出异常,只是返回 null。因此新的方法更适合容易出现异常条件的情况。 peek,element区别: element() 和 peek() 用于在队列的头部查询元素。与 remove() 方法类似,在队列为空时, element() 抛出一个异常,而 peek() 返回 null 来源: https://www.cnblogs.com/doyi111/p/11577402.html

Is there a “HasNext” method for an IEnumerator?

最后都变了- 提交于 2019-11-30 08:59:36
With Java Iterator s, I have used the hasNext method to determine whether an iteration has more elements (without consuming an element) -- thus, hasNext is like a " Peek " method. My question: is there anything like a " hasNext " or " Peek " method with C#'s generic IEnumerator s? No, unfortunately there isn't. The IEnumerator<T> interface only exposes the following members: Methods: Dispose MoveNext Reset Properties : Current No, but in C# you can repeatedly ask for the current element without moving to the next one. It's just a different way of looking at it. It wouldn't be too hard to write

Is StreamReader.Readline() really the fastest method to count lines in a file?

巧了我就是萌 提交于 2019-11-30 06:51:53
While looking around for a while I found quite a few discussions on how to figure out the number of lines in a file. For example these three: c# how do I count lines in a textfile Determine the number of lines within a text file How to count lines fast? So, I went ahead and ended up using what seems to be the most efficient (at least memory-wise?) method that I could find: private static int countFileLines(string filePath) { using (StreamReader r = new StreamReader(filePath)) { int i = 0; while (r.ReadLine() != null) { i++; } return i; } } But this takes forever when the lines themselves from

Get the number of bytes available in socket by 'recv' with 'MSG_PEEK' in C++

好久不见. 提交于 2019-11-30 02:05:35
C++ has the following function to receive bytes from socket, it can check for number of bytes available with the MSG_PEEK flag. With MSG_PEEK , the returned value of 'recv' is the number of bytes available in socket: #include <sys/socket.h> ssize_t recv(int socket, void *buffer, size_t length, int flags); I need to get the number of bytes available in the socket without creating buffer (without allocating memory for buffer ). Is it possible and how? You're looking for is ioctl(fd,FIONREAD,&bytes_available) , and under windows ioctlsocket(socket,FIONREAD,&bytes_available) . Be warned though,

LeetCode 0155 -- 最小栈

懵懂的女人 提交于 2019-11-29 21:25:03
最小栈 题目描述 设计一个支持 push,pop,top 操作,并能在常数时间内检索到最小元素的栈。 push(x) – 将元素 x 推入栈中。 pop() – 删除栈顶的元素。 top() – 获取栈顶元素。 getMin() – 检索栈中的最小元素。 示例: MinStack minStack = new MinStack(); minStack.push(-2); minStack.push(0); minStack.push(-3); minStack.getMin(); --> 返回 -3. minStack.pop(); minStack.top(); --> 返回 0. minStack.getMin(); --> 返回 -2. 解题思路 个人AC 每次将元素压栈后,需要根据情况更新最小元素。 class MinStack { private Stack < Element > stack ; private class Element { int x ; int min ; Element ( int x , int min ) { this . x = x ; this . min = min ; } } /** initialize your data structure here. */ public MinStack ( ) { this . stack =

225. 用队列实现栈

ぐ巨炮叔叔 提交于 2019-11-29 19:14:37
使用队列实现栈的下列操作: push(x) -- 元素 x 入栈 pop() -- 移除栈顶元素 top() -- 获取栈顶元素 empty() -- 返回栈是否为空 注意: 你只能使用队列的基本操作-- 也就是 push to back, peek/pop from front, size, 和 is empty 这些操作是合法的。 你所使用的语言也许不支持队列。 你可以使用 list 或者 deque(双端队列)来模拟一个队列 , 只要是标准的队列操作即可。 你可以假设所有操作都是有效的(例如, 对一个空的栈不会调用 pop 或者 top 操作)。 class MyStack { LinkedList<Integer> queue1 = new LinkedList<Integer>(); /** * Initialize your data structure here. */ public MyStack() { } /** * Push element x onto stack. */ public void push(int x) { queue1.addLast(x); } /** * Removes the element on top of the stack and returns that element. */ public int pop() {

232. 用栈实现队列

房东的猫 提交于 2019-11-29 16:56:09
使用栈实现队列的下列操作: push(x) -- 将一个元素放入队列的尾部。 pop() -- 从队列首部移除元素。 peek() -- 返回队列首部的元素。 empty() -- 返回队列是否为空。 示例: MyQueue queue = new MyQueue(); queue.push(1); queue.push(2); queue.peek(); // 返回 1 queue.pop(); // 返回 1 queue.empty(); // 返回 false 说明: 你只能使用标准的栈操作 -- 也就是只有 push to top, peek/pop from top, size, 和 is empty 操作是合法的。 你所使用的语言也许不支持栈。你可以使用 list 或者 deque(双端队列)来模拟一个栈,只要是标准的栈操作即可。 假设所有操作都是有效的 (例如,一个空的队列不会调用 pop 或者 peek 操作)。 自己尝试解题 ok public class MyQueue { Stack<Integer> s1 = new Stack<>(); Stack<Integer> s2 = new Stack<>(); /** * Initialize your data structure here. */ public MyQueue() { } /** *

刷题笔记《剑指offer》-第二十题 包含min函数的栈

做~自己de王妃 提交于 2019-11-29 06:02:34
题目描述: 定义栈的数据结构,请在该类型中实现一个能够得到栈中 所含最小元素的min函数(时间复杂度应为O(1))。 思路: 1. 使用一个辅助栈 2. 初始时,都为空 3. 放入第一个元素时,数据栈和辅助栈都放入同一个元素 4. 当放入第二个甚至更多个的元素时,数据栈正常放入数据,辅助栈中放入较小的数据 5. 弹栈都同时弹出栈顶元素 6. 辅助栈的栈顶一直都是最小元素 代码 public class StackWithMin { private Stack<Integer> data_s = new Stack<>(); private Stack<Integer> min_s = new Stack<>(); public void push(int node) { if(min_s.empty()) { min_s.push(node); } else { if(node < min_s.peek()) { min_s.push(node); } else min_s.push(min_s.peek()); } data_s.push(node); } public void pop() { data_s.pop(); min_s.pop(); } public int top() { return data_s.peek(); } public int min() {