关于cin的问题 [英] question about cin

查看:90
本文介绍了关于cin的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在第213页的Stanley Lippman的C ++ Primer(第四版)中找到了以下代码片段




string inBuf;


while(cin> inBuf&&!inBuf.empty())

{

//做某事
}


这是需要的条件检查(!inBuf.empty())?

是否有可能表达式(cin> ; inBuf)是真的但是仍然

inBuf是空的?


请解释一下。

解决方案

嗯...这对我来说似乎是一些非常奇怪的编码,但是我会将b $ b扩展到这个:


(1)

{

cin> inBuf;

if(inBuf!= null)

{

//做点什么

}

其他休息;

}

//程序结束


基本上,程序得到输入。如果输入不为null,则

继续处理。如果为null,则结束。这可能是一个例子

的用法:


while(cin>(int)integerValue&& integerValue!= null)

{

counter + = inegerValue;

cout<<计数器<< " \ n";

}

cout<< \ n \ n总数是: <<计数器;


它本质上是一个空终止循环。 _I think_


su **************@yahoo.com ,印度写道:


我发现以下代码片段在C ++ Primer(第四版)

作者:Stanley Lippman,第213页。


string inBuf;


while( cin> inBuf&&!inBuf.empty())

{

//做点什么

}


这是需要的条件检查(!inBuf.empty())?

表达式(cin> inBuf)可能是真的但仍然是

inBuf是空的?



让我们看看。 cin> inBuf中的部分计算为cin,然后通过

void *转换为bool。值为!(cin.fail())。但是,如果提取

运算符实际上并没有提取一些运算符,它将设置failbit

[见21.3.7.9/2a]因此,如果cin>> inBuf转换为true,我们知道

failbit未设置,因此至少提取了一个字符。


因此,上面的循环应该相当于:


string inBuf;

while(cin> inBuf){

assert(!inBuf.empty()); // [见21.3.7.9/2a]

//做点什么

}

最好


Kai-Uwe Bux


6月19日上午9:49,Kai-Uwe Bux< jkherci ... @ gmx.netwrote:
< blockquote class =post_quotes>
让我们看看。 cin> inBuf中的部分计算为cin,然后通过

void *转换为bool。值为!(cin.fail())。但是,如果提取

运算符实际上并没有提取一些运算符,它将设置failbit

[见21.3.7.9/2a]因此,如果cin>> inBuf转换为true,我们知道

failbit未设置,因此至少提取了一个字符。


因此,上面的循环应该相当于:


string inBuf;

while(cin> inBuf){

assert(!inBuf.empty()); // [见21.3.7.9/2a]

//做点什么

}


Best


Kai-Uwe Bux



由于我是初学者,我的疑问仍然存在。正如你所提到的那样,如果(cin>>

inBuf)是真的,那么至少会提取一个字符,这意味着inBuf是非空;不是吗?

那么为什么我们要求检查

(!inBuf.empty())。


请解释


谢谢

V.Subramanian


I found the following code fragment in the C++ Primer(Fourth Edition)
by Stanley Lippman in page 213.

string inBuf;

while (cin >inBuf && ! inBuf.empty( ) )
{
// do something
}

Here is the condition check ( ! inBuf.empty() ) needed ?
is it possible that the expression ( cin >inBuf ) is true but still
inBuf is empty ?

Kindly explain.

解决方案

Hmmmm... This seems like some very odd coding to me, however I would
expand it to this:

while (1)
{
cin >inBuf;
if (inBuf != null)
{
// do something
}
else break;
}
// end of program

Basically, the program gets input. If the input is not null it
continues to process. If it is null, it ends. Here might be an example
of usage:

while (cin >(int) integerValue && integerValue != null )
{
counter += inegerValue;
cout << counter << "\n";
}
cout << "\n\nThe total is: " << counter;

It is essentially a null terminated loop. _I think_


su**************@yahoo.com, India wrote:

I found the following code fragment in the C++ Primer(Fourth Edition)
by Stanley Lippman in page 213.

string inBuf;

while (cin >inBuf && ! inBuf.empty( ) )
{
// do something
}

Here is the condition check ( ! inBuf.empty() ) needed ?
is it possible that the expression ( cin >inBuf ) is true but still
inBuf is empty ?

Let''s see. The part cin >inBuf evaluates to cin which then converts via
void* to bool. The value is !(cin.fail()). However, if the extraction
operator does not actually extract some operators, it will set the failbit
[see 21.3.7.9/2a] Thus, if cin>>inBuf converts to true, we know that the
failbit was not set and thus at least one character was extracted.

Hence, the loop above should be equivalent to:

string inBuf;
while ( cin >inBuf ) {
assert( ! inBuf.empty() ); // [see 21.3.7.9/2a]
// do something
}
Best

Kai-Uwe Bux


On Jun 19, 9:49 am, Kai-Uwe Bux <jkherci...@gmx.netwrote:

Let''s see. The part cin >inBuf evaluates to cin which then converts via
void* to bool. The value is !(cin.fail()). However, if the extraction
operator does not actually extract some operators, it will set the failbit
[see 21.3.7.9/2a] Thus, if cin>>inBuf converts to true, we know that the
failbit was not set and thus at least one character was extracted.

Hence, the loop above should be equivalent to:

string inBuf;
while ( cin >inBuf ) {
assert( ! inBuf.empty() ); // [see 21.3.7.9/2a]
// do something
}

Best

Kai-Uwe Bux

My doubt still persists since I am a beginner. As you have mentioned
that at least one character would have been extracted if (cin >>
inBuf) were to be true, it means that inBuf is non-empty; isn''t it ?
Then why do we require the checking
( ! inBuf.empty() ).

Kindly explain

Thanks
V.Subramanian


这篇关于关于cin的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆