C++ Primer 第五版 第四章 表达式

4.1
105

4.2
(vec.begin())
(
(vec.begin()))+1

4.7
溢出:当计算的结果超出该类型所能表示的范围时就会产生溢出。

4.8
短路求值

4.9

1.p是否是空指针
2. 如果非空指针,那么所指向的对象是否是空字符串

4.12
如果i不是布尔值,则判断i是否不等于1;
否则,判断i是否是真,也就是非零。

4.13
在这里插入图片描述
4.17
前置递增运算符把值加1后直接返回改变了的运算对象;
后置递增运算符需要将原始存储下来以便于返回这个未修改的内容。

4.18
越界

4.20
b、e:字符常量不支持++运算符;
c:指针不包含任何成员;

4.21

  1 #include <iostream>
  2 #include <vector>
  3 using namespace std;
  4 
  5 int main()
  6 {       
  7         vector<int> it(10, 1);
  8         
  9         for (auto &c : it)
 10                 c = ((c % 2 == 0) ? c : 2 * c);
 11         cout << endl;
 12         
 13         for (auto &c : it)
 14                 cout << c << ' ';
 15         cout << endl;
 16 }
 17 

4.22
条件运算符跟简洁,if语句更容易理解

  1 #include <iostream>
  2 using namespace std;
  3 
  4 int main()
  5 {
  6         int finalgrade = 70;
  7         cout << ((finalgrade > 90) ? "high pass": (finalgrade > 75) ? "pass" : (finalgrade > 60) ? "low pass" : "fail") ;
  8         cout << endl;
  9 
 10         //if省略
 11         return 0;
 12 }

4.23
==时,char类型无法跟string类型对象相匹配。

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 int main()
  6 {
  7         string s = "word";
  8         string p1 = s + (s[s.size() - 1] == 's' ? "" : "s");
  9 }

4.25
7232

  1 #include <iostream>
  2 using namespace std;
  3 
  4 int main()
  5 {       
  6         cout << ('q' << 6) << endl;
  7         return 0;
  8 }

4.29
10
2

  1 #include <iostream>
  2 using namespace std;
  3 
  4 int main()
  5 {       
  6         int x[10];
  7         int *p = x;
  8         cout << sizeof(x)/sizeof(*x) << endl;
  9         cout << sizeof(p) /sizeof(*p) << endl;
 10 }   

4.37

  1 #include <iostream>
  2 #include <string>
  3 using namespace std;
  4 
  5 int main()
  6 {
  7         int i;
  8         double d;
  9         const string *ps;
 10         char *pc;
 11         void *pv;
 12 
 13         pv = static_cast<void*>(const_cast<string*>(ps));
 14         i = static_cast<int>(*pc);
 15         pv = static_cast<void*>(&d);
 16         pc = reinterpret_cast<char*>(pv);
 17 }
~                                                                                                                                                                   

4.38
将j/i结果强制转换为double类型;

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值