c++ primer 第四章

主要巩固知识,多用几种方法实现

4.20

#include<iostream>
#include<vector>
#include<string>
using namespace::std;
int main()
{
    string arr[] = { "hello",",","how","are","you" };
    vector<string>arrow(begin(arr), end(arr));
    //尽量使用前置
    for (vector<string>::iterator iter = arrow.begin(); iter != arrow.end(); ++iter){
        //*iter++;  合法: 先对iter加1,再返回iter指向的值
        //(*iter)++; 不合法: 返回iter指向的值为string++操作
        //*iter.empty(); 不合法:  iter是个指针,没有empty()的成员
        //iter->empty();  合法:判断iter所指向的值是否为空
        //++*iter;   不合法: *iter可以得到iter所指的字符串,但是字符串并没有++操作
        //iter++->empty();  合法: 首先判断iter所指向的值是否为空,再对iter加1
        cout << *iter << endl;
    }
    return 0;
}

注意:

iter++->empty()   先比较iter->empty,再进行iter++

++iter->empty()   比较iter->empty,无法执行++iter;不合法,表达式必须是可修改的左值

4.21

#include<iostream>
#include<vector>
#include<string>
using namespace::std;
int main()
{
    int num;
    vector<int> m{ 10,9 };
    m.begin();
    while (cin >> num)
    {
        m.push_back(num);
    }
    for (auto &c : m) {
        if (c%2)
            c = c * 2;
    }
    //for (auto &iter = m.begin(); iter != m.end();++iter) 错误非常量引用必须是左值
    for (auto iter = m.begin(); iter != m.end();++iter) {
        cout << *iter << " ";
    }
    return 0;
}

4.22

ctral+z再enter跳出循环

#include<iostream>
#include<vector>
#include<string>
using namespace::std;
using std::vector;
int main()
{
    int grade;
    cout << "please Input the grade:" << endl;
    while (cin >> grade)
    {
        string finalgrade;
    //注意=号 finalgrade
= (grade < 60) ? "fail" : (grade < 75) ? "low pass" : (grade <=90) ? "pass" : "hign pass"; cout << finalgrade << endl; } return 0; }

4.23

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

4.24

嵌套条件运算符

满足右结合律,输入95

判断过程; (grade > 90)->输出“high pass”

若满足左结合律,输入95

判断过程:(grade > 90)->(grade < 60)-> 输出“pass”

    先判断(grade>90)

4.25

4.26

4.27

(a)3

(b)7

(c)1

(d)1

4.28

#include<iostream>
#include<string>
using namespace::std;
int main() {
    double p = 2;
    int * q ;
    //cout << sizeof(q) << endl;
    cout << sizeof(int *) << endl;
    return 0;
}

4.29

#include<iostream>
#include<string>
using namespace::std;
int main() {
    int x[10]; int *p = x;
    cout << sizeof(x) << endl;//40
    cout << sizeof(x) / sizeof(*x) << endl;//10
    cout << sizeof(p) / sizeof(*p) << endl;//1
    return 0;
}

4.30

(a) sizeof (x+y)  错误演示:(sizeof x)+y

(b) sizeof (p->men[i])

(c) sizeof (a<b)  或者((sizeof x) > y)

(d) sizeof (f())

int x = 3, y = 5;
    cout << sizeof(x > y);//1
    cout << sizeof (bool);//1

4.33

#include<iostream>
#include<string>
using namespace::std;
int main() {
    int somevalue,val;
    /*
    while (cin >> somevalue)
    {
        int x = 10, y = 20;//局部变量方便检验
        somevalue ? ++x, ++y : --x, --y;//(somevalue ? ++x, ++y : --x), --y;
        cout << somevalue <<" " << x << " " << y ;
    }
    */
    //修改后
    while (cin >> somevalue)
    {
        int x = 10, y = 20;
        somevalue ? ++x, ++y : (--x, --y);
        cout << somevalue << " " << x << " " << y;
    }
    return 0;
}
原文地址:https://www.cnblogs.com/addicted-to-you/p/10671887.html