C++ Primer 4章习题

4.1 105
4.2
(a)*(vec.begin())
(b)(*vec.begin())+1
4.3 略
4.4 12/3*4+5*15+24%4/2=91
4.5
(a)-86
(b)-18
(c)0
(d)-2
4.6
if(num%2){}
else{}
4.7 要表达的值超出该类型的取值范围。
4.8
&&/||:先求左侧表达式再求右侧表达式。左侧不符合需求时才求右侧的值
==:未定义
4.9 cp不是空指针,且*cp不是空字符串
4.10

int main(int argc, char* argv[])
{
    int temp;
    while (cin >> temp&&temp!=42)
        cout << temp << endl;
    return 0;
}

4.11 a>b&&b>c&&c>d
4.12 先求j< k的结果,检测此结果是否等于i
4.13 (a)i=3,d=3
(b)i=3,d=3.5
4.14 if(42=i)//报错
if(i=42)//永远执行
4.15 pi是指针,不能给整型变量赋值
4.16 少了括号
将赋值语句作为了条件判断表达式
4.17 前置递增运算符,将对象加一后返回加一之后的值
后置递增运算符,将对象加一后返回加一之前的值
4.18 无法处理第一个值,会解引用一个不存在的值
4.19 (a)ptr不是空指针且ptr所指的int变量不为0,ptr指向下一对象
(b)ival是否为0,ival加一
(c)未定义
4.20 a,b,d,e,f合法c不合法
4.21

int main(int argc, char* argv[])
{
    vector<int> stupid;
    int temp;
    while (cin >> temp&&temp!=42)
        stupid.push_back(temp);
    for (auto c : stupid)
        cout << c << ' ';
    cout << endl;
    for (auto &c : stupid)
        c = c % 2 ? c * 2 : c;
    for (auto c : stupid)
        cout << c << ' ';
    cout << endl;
    return 0;
}

4.22 cout<<(grade<60)?”fail”:(grade<75?)”low pass”:(grade<=90?)”pass”:”high pass”;
4.23 条件运算符优先级低,判断条件表达式永远有效
4.24 反过来
4.25 10000000
4.26 溢出
4.27 (a)0x00000003&0x00000007=0x00000003
(b)0x00000003|0x00000007=0x00000007
(c)0x00000003&&0x00000007=true
(d)0x00000003||0x00000007=true
4.28

int main(int argc, char* argv[])
{
    cout << "int" << sizeof (int) << endl;
    cout << "float" << sizeof(float) << endl;
    cout << "double" << sizeof(double) << endl;
    cout << "unsigned int" << sizeof(unsigned int) << endl;
    return 0;
}

4.29
sizeof测量数组时,会给出数组中所有成员占用的内存大小。
sizeof测量指针时,只给出指针所占用内存大小。
4.30
(a)sizeof(x+y)
(b)sizeof(p->mem[i])
(c)sizeof(a

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值