- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
当我将 cin.peek() 与“char”数据类型一起使用时,它工作得很好,但它不适用于“string”数据类型。这很好用:
#include<iostream>
using namespace std;
int main(){
cout<<"enter a word"<<endl;
char a;
cin>>a;
if(cin.peek()=='c'){
cout<<"ha"<<endl;
}
return 0;
}
如果输入是“dce”它打印“ha”但是 下面的代码不做同样的工作:
#include<iostream>
#include<string>
using namespace std;
int main(){
cout<<"enter a word"<<endl;
string a;
cin>>a;
if(cin.peek()=='c'){
cout<<"ha"<<endl;
}
return 0;
}
是不是字符串数据类型更合适,因为我们将单词存储在“a”变量中。 “char”数据类型可以用来存储单词还是只能存储单个字母?
最佳答案
cin.peek()
返回输入序列中的下一个字符,而不提取它:该字符保留为要从流中提取的下一个字符。
对于你的两个代码,输入是 dce
在第一个代码中
char a;
cin>>a
它只从输入缓冲区中获取 d
而 ce
仍在其中,因此当您执行 cin.peek()
时,它会得到缓冲区中的下一个字符是 c
。但是对于第二个代码
string a;
cin>>a;
由于数据类型是字符串,它从输入流缓冲区中获取整个 dce
,当您执行 cin.peek()
时,它会检查流中是否有任何内容但现在没有了。因此它返回 EOF
If there are no more characters to read in the input sequence, or if any internal state flags is set, the function returns the end-of-file value (EOF), and leaves the proper internal state flags set:
关于c++ - cin.peek() 在 C++ 中如何工作?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43993795/
我有 PriorityQueue 使用示例,它产生 3 1 1 1 5 0 这是代码 import java.util.*; class Someclass { public static class
这个问题在这里已经有了答案: Help: ZX81 BASIC "Peek" function (3 个回答) 7年前关闭。 我需要一种方法来查找字符 ('即将移动,然后阅读 LET C= PEEK(
在 Facebook 应用程序中,如果您“偷看”相册中的一张照片,您可以水平滑动手指以转到其他照片。 这是如何在 API 中完成的?我所看到的只是提供一个 View Controller 并在弹出时提
我对MassTransit使用者是否可以在实际检索msg之前可以Peek()一个MSMQ队列感到好奇。 步骤/过程是什么: 1)消息已发送到队列 2)消费者获取它并必须进行数据库更新-大约需要5秒钟
我从 Microsoft docs 了解到在第一个 Peek() 操作期间,任何一个可用的消息代理都会响应并发送其最旧的消息。然后在后续的 Peek() 操作中,我们可以遍历分区来查看序列号增加的每条
嗨,我正在尝试查看队列,但编译器不断抛出找不到符号错误。 public class HelloWorld{ interface Queue { Queue enqueue(T ele); T dequ
我在循环中遇到了各种场景,在迭代处理项目时我会向前“查看”或“跳过”。 一种情况是我正在枚举文件的行,并且行末尾有一个“继续”字符,指示将下一行与当前行合并。如果我只是简单地循环,这并不太难,我可以读
首先,我想说如果有人能在这里提供帮助,你真是太棒了。 一般问题 我的Python程序需要与MSMQ交互。基本上,我想查看队列,如果队列中没有任何内容,则指定超时。 但是,尽管我尽了最大努力,当队列中先
我正在学习 C 和 C#,这个问题是针对 C# 的。我有这个 while 循环,它导致无限循环。我以前用过这个,它总是很好用。但现在它只是永远循环,永远不会退出。我正在执行此 while 循环以计算文
我在从文件中读取输入时遇到了一些问题,我真的不应该在家庭作业中遇到这些问题。我在之前的上一次分配中使用了非常相似的代码(唯一改变的是值被插入的地方)。我得到了一个包含以下输入的文本文件: 10 3 6
istream& operator>>(istream& s, vector& ord) { string c{}; bool got_customer = false; Cu
Eventfd维护一个 uint64_t 计数器,每次写入时都会增加,每次读取时都会重置为零(如果用作 EFD_SEMAPHORE,则减 1)。是否可以在不更改 eventfd 内部计数器值的情况下查
假设我有一个外部 while 循环来读取每个字符并将其输出到控制台。如果找到一个词,我还想标记它,通过使用 peek 方法,我可以找到一个词的第一个实例。有没有办法窥视前方的多个地方。例如,如果我要查
LinkedList 具有方便的peek、pop、...方法。 不幸的是,我需要一个线程安全的LinkedList。所以,我的第一个想法是将其包装如下: List list = Collections
对于我的应用程序,某些 fragment 要求用户在显示之前在对话框中输入 PIN。我有一个 BaseFragment 类,如果需要 pin,我的所有其他 fragment 都会扩展该类。我现在的问题
我的问题以最简单的方式表达: 根据 JavaDoc : Peek() method exists mainly to support debugging, where you want to see
关闭。这个问题不符合Stack Overflow guidelines .它目前不接受答案。 这个问题似乎不是关于 a specific programming problem, a softwar
本文将介绍 Stream.peek 方法示例。 peek 方法返回包含该流元素的流,并对每个元素执行提供的 Consumer 操作。 peek 是一个中间操作。从 Java 文档中查找 peek 方法
这似乎应该很容易做到,但是每次我处理这个问题时,我最终都会得到感觉“不太优雅”的解决方案 这是我的基本问题:如果我正在循环浏览以特定方式订购的字典,在循环中的任何给定点内,我如何“查看”或获得对当前位
List nums = Arrays.asList(1,2,3,4,5,6); long result = nums .stream() .peek(System.ou
我是一名优秀的程序员,十分优秀!