4.7

4.21

#include <iostream>
#include <vector>

using namespace std;
int main()
{
    vector<int> ivec{ 1, 2, 3, 4, 5, 6, 7, 8, 9 };

    for (auto i : ivec) {
        cout << ((i % 2) ? i : i * 2) << " ";
    }
    cout << endl;

    return 0;
}

4.22

#include <iostream>
using namespace std;

int main()
{
    for (unsigned g; cin >> g; )
    {

        auto result = g > 90 ? "high pass" : g < 60 ? "fail" : g < 75 ? "low pass" : "pass";
        cout << result << endl;


        if (g > 90)         cout << "high pass";
        else if (g < 60)    cout << "fail";
        else if (g < 75)    cout << "low pass";
        else                cout << "pass";
        cout << endl;
    }

    return 0;
}

if更容易理解 ,但是用条件运算符,代码会简洁很多,倾向于使用条件运算符

4.23

string s = "word";
string pl = s + s[s.size() - 1] == 's' ? "" : "s" ;

改正:

string pl = s + (s[s.size() - 1] == 's' ? "" : "s") ;

当你不确定优先级的时候,请使用括号来明确表达你的意图

4.24
boring

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值