- android - RelativeLayout 背景可绘制重叠内容
- android - 如何链接 cpufeatures lib 以获取 native android 库?
- java - OnItemClickListener 不起作用,但 OnLongItemClickListener 在自定义 ListView 中起作用
- java - Android 文件转字符串
我的问题是如何,
while(cin>>x)
{
//code
}
工作。或者更具体地说,该代码如何停止循环?
来自文档 here ,看起来 >> 运算符返回一个 &istream
。这是否意味着如果读取失败或到达文件末尾,它不仅会设置 eofbit、failbit 或 badbit,还会返回空值?这真的没有意义,所以我怀疑是这样。
是否对 eofbit 进行某种隐式检查?
我问是因为我希望用这样的 2 个类实现类似的东西,
class B
{
//variables and methods
}
class A
{
//Variables and methods
//Container of B objects. ex. B[] or vector<B> or Map<key,B>
&A >> (B b);
}
int main()
{
A a;
B b;
while( a >> b)
{
//Code
}
}
注意:我不打算从 istream
继承,除非它是使这项工作变得神奇的地方。这样做的原因是我希望让类及其依赖项尽可能小。如果我从 istream
继承,我将收到它的所有公共(public)和 protected 东西,并且我不会尝试创建类似 istream
的对象。我只想复制一个真的不错的部分。
编辑: 我正在使用 Visual Studio 2010(这真的很痛苦),我需要一些与它的 C++03 + 一些 C++ 实现兼容的东西11.
最佳答案
From the documentation here, it looks like the >> operator returns a &istream. Does that mean if the read fails or hits the end of file it not only sets the eofbit, failbit or badbit but also returns a null?
不,返回对流对象的引用,但将设置内部标志。随后调用 operator>>
仅测试这些标志后就会失败。
Is there some kind of implicit check of the eofbit?
不确定问题是什么。流不会尝试预先确定下一个请求是否会失败,而只会在一个操作因为遇到 EOF 而失败时设置标志。每次调用operator>>
将首先测试流的状态,只有在流处于良好状态时才会继续。
How does
while(cin>>x)
work?
存在从流对象到 bool 值的隐式转换。转换产量 true
如果流处于良好状态或 false
否则。
关于C++ While( cin >> x) 是如何工作的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12923762/
我使用 cin.peek() 方法获得了这段代码。我注意到奇怪的行为,当程序输入看起来像 qwertyu$[Enter] 时,一切正常,但是当它看起来像 qwerty[Enter]$ 时,它仅在我键入
#include #include using namespace std; int main(){ ofstream fout("student.dat",ios::out); char name[
我是 C++ 编程新手,遇到了障碍。这是我的代码: #include using namespace std; int main(){ int sum = 0, a; cout >
我有一个简单的程序,当输入完全符合预期时,它名义上可以工作。 #include using namespace std; int main () { int a, b; char a
int main () { int num1, num2; int cnt = 1; if (cin >> num1){ while (cin >> num2){
我有这个程序,但 cin 随机跳过..我的意思是有时它会,有时它不会。有什么解决办法吗? int main(){ /** get course name, number of
我是一名正在准备期末考试的 C++ 初学者。我用两种方式写了一个程序。第一个代码使用 cin.getline() 并且不能正常工作。第二个代码使用 cin.get() 和 cin >> 并正确执行所有
据我所知,在 cin 之后使用 getline() 时,我们需要先刷新缓冲区中的换行符,然后再调用 getline(),我们通过调用 cin.ignore() 来完成此操作。 std::string
我正在尝试以下代码: int main() { char str1[20]; int a; cout > a; cout > 忽略空格(即 ' ', '\t', '\n
我刚开始阅读 C++ 11 入门书,目前在 if 语句部分。我以前有使用 Java 的经验,但 C++ 输入/输出确实让我感到困惑。为了方便起见,我将多个练习放在一个文件中。但是,似乎自从我调用了 w
这个问题在这里已经有了答案: 关闭 10 年前。 Possible Duplicate: c++ cin input not working? 我一直在尝试使用以下 C++ 代码在整数后输入字符串。
需要 I/O 重定向,我不能使用 fstream。我用 I/O通过使用 ">line)通过 I/O 读取文件重定向。然后我需要 cin>>x ,但这次是在运行时进行用户输入,但会被跳过。 我试过了 c
我正在尝试使用 getline() 但在输入第一个记录光标后不要等待第二个国家/地区名称,它会跳过并跳转到首都名称。我的错误在哪里。如果我输入 国家:印度 首都:德里 Capita:57382 它可以
我有以下循环。它应该读取数字直到 EndOfFile,或者用户输入 -999 int arr[100]; int index; for (index = 0; index > arr[index];
以下两个循环和每个循环什么时候停止有什么区别? #include #include #include using namespace std; int main() { int x,y;
在这个程序的每次输入中,我希望用户能够输入他们想要的任意数量的“单词”(意思是用空格/制表符分隔的文本)......但是不允许用户输入单词超出当前 cin。例如:如果我键入 John Smith Do
int main() { int oddeven = 1; int modulotwo; // Simple loop since no reason to check 0
我写了一个 switch 语句,并创建了一个默认值,它会简单地说用户选择了一个错误的选项并重复输入。我想确保如果出现问题,它会首先清除缓冲区,所以我使用了 cin.sync(),但输入 'a' 仍然会
我是一个新手程序员,正在练习下面的这个程序。无论出于何种原因,在我的程序结束时,cin.ignore() 被编译器完全跳过并直接移动到 cin.get(),但是之前已经有一个按键,所以编译器完全跳过它
关闭。这个问题需要debugging details .它目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and th
我是一名优秀的程序员,十分优秀!