首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > C++ >

关于if(b);与if(b==true);的差异

2013-09-06 
关于if(b)与if(btrue)的区别#include iostream#include string#include cctypeusing namespace

关于if(b);与if(b==true);的区别

#include <iostream>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string s,result_str;
bool has_punct=false;
char ch;
cout<<"输入一段文字"<<endl;
getline(cin,s);
for(string::size_type index=0;index<s.size();index++)
{
ch=s[index];
if(ispunct(ch)==true)
{
has_punct=true;
}
else
result_str+=ch;
}
if(has_punct)
cout<<result_str;
else
cout<<s;
return 0;
}

第十五行中,if(ispunct(ch)==true)测试的结果:输入s,,s,,得出s,,s,,
而改为if(ispunct(ch))测试的结果为:输入s,,s,,得出ss
我的问题是if(ispunct(ch)==true);和if(ispunct(ch));有区别吗,还是我的编译器的问题,望大家帮忙解答一下,答者有分。 编译器 string c++ 判断语句 bool
[解决办法]
看ispunct函数的返回值
int ispunct(int c)
检查参数c是否为标点符号或特殊符号。返回TRUE也就是代表参数c为非空格、非数字和非英文字母。

这样看if(ispunct(ch)==true)和if(ispunct(ch));没有区别的
[解决办法]
Synopsis:

#include <stdio.h>

int ispunct(int c); 

Description:

The function returns nonzero if c is any of:

! " # % &  ' ( ) ; < = >> ? 
[ \ ] * + , - . / : ^ _ { 
[解决办法]
 } ~

Return Value

The function returns nonzero if c is punctuation otherwise this will return zero which will be equivalent to false.
Example

#include <stdio.h>

int main() {

  if( ispunct( '%' ) )
  {
     printf( "Character  % is a punctuation\n" );
  }
  if( ispunct( 'A' ) )


  {
     printf( "Character  A is a punctuation\n" );
  }
  return 0;
}

It will proiduce following result:

Character  % is a punctuation


[解决办法]
前者是正常人的行径,后者是懵懂少年的行为。
[解决办法]
好问题.
ispunct(ch)==true
vs下最后生成的比较的代码是:
cmp         eax,1 

ispunct(ch)
这个生成的是:
test        eax,eax

所以非0即为真这个命题好像不成立?
另外vs的编译器会给出警告
warning C4805: '==' : unsafe mix of type 'int' and type 'bool' in operation
[解决办法]
WG21/N3691
3.9/6 Values of type bool are either true or false.48 [ Note: There are no signed, unsigned, short, or long
bool types or values. —end note ] Values of type bool participate in integral promotions (4.5).
48) Using a bool value in ways described by this International Standard as “undefined,” such as by examining the value of an
uninitialized automatic object, might cause it to behave as if it is neither true nor false.
4/4 Certain language constructs require that an expression be converted to a Boolean value. An expression e
appearing in such a context is said to be contextually converted to bool and is well-formed if and only if the
declaration bool t(e); is well-formed, for some invented temporary variable t (8.5).
4.12/1 A prvalue of arithmetic, unscoped enumeration, pointer, or pointer to member type can be converted to a
prvalue of type bool. A zero value, null pointer value, or null member pointer value is converted to false;
any other value is converted to true. For direct-initialization (8.5), a prvalue of type std::nullptr_t can
be converted to a prvalue of type bool; the resulting value is false.


6.4/4 The value of a condition that is an initialized declaration in a statement other than a switch statement is the
value of the declared variable contextually converted to bool (Clause 4). If that conversion is ill-formed, the
program is ill-formed. The value of a condition that is an initialized declaration in a switch statement is the
value of the declared variable if it has integral or enumeration type, or of that variable implicitly converted
to integral or enumeration type otherwise. The value of a condition that is an expression is the value of the
expression, contextually converted to bool for statements other than switch; if that conversion is ill-formed,
the program is ill-formed. The value of the condition will be referred to as simply “the condition” where the
usage is unambiguous.

[解决办法]
因为是int所以等效,不等效的情况是你代码引起未定义行为。不过还是尽量别多此一举==true。
如果b是定义了explicit bool()但没定义和bool之间的==操作的类型,if(b)没问题而if(b == true)就不行。


[解决办法]
区别就是 if (ispunct) 对所有非零值都成立;而 if (ispunct==true) 只有当 ispunct 返回值为 1 的时候才成立,这是因为 == 要求其操作数发生 integral promotion,所以 true 变成了 1,作为一个整形数字参与比较,就好像写 if (ispunct == 1),错误显而易见。

热点排行