zoukankan      html  css  js  c++  java
  • 第四章 表达式

    4.17

    传送门

    4.18

    *++pbeg:先将pbeg加1,然后用加1后的pbeg作为解引用运算符的运算对象。

    所以,会从vector的第二个元素开始输出,直至输出最后一个元素的后一个位置(未知)。

    4.19

    题目意思是:int ival, *ptr;  vector<int> vec;

    (a):判断ptr是否为空指针,若不是,再判断ptr指向的对象的值是否为0

    (b):ival的值是否为0,若不是,再判断它加1后的值是否为0

    (c):求值顺序不确定,没有意义的表达式,修改为“vec[ival] <= vec[ival+1]”

    4.20

    iter是vector<string>类型的迭代器,它指向的对象是string类型。

    (a):合法,先加1,再解引用此时指向的string对象

    (b):非法,string对象自加??

    (c):非法,解引用运算符的优先级低于点运算符,而iter没有该成员函数

    (d):合法,->运算符搭配迭代器使用,表示迭代器所指对象的成员

    (e):非法,同(b)

    (f ):合法,先判string对象是否为空,再将迭代器加1

    4.21

    #include <iostream>
    #include <vector>
    #include <cctype>
    #include <iterator>
    #include <string>
    #include <cstring>
    
    using std::cin;
    using std::cout;    
    using std::endl;
    using std::vector;
    using std::string;
    
    int main() 
    {
    	vector<int> a = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    	for(auto &i : a)
    		i = i%2 ? 2*i : i;
        return 0;
    }
    

      

    4.22

    #include <iostream>
    #include <vector>
    #include <cctype>
    #include <iterator>
    #include <string>
    #include <cstring>
    
    using std::cin;
    using std::cout;    
    using std::endl;
    using std::vector;
    using std::string;
    
    int main() 
    {
    	int grade = 70;
    	string level = (grade > 90) ? "high pass" : (grade < 60) ? "fail" : (grade < 75) ? "low pass" : "pass";
    	cout << level << endl;
        return 0;
    }
    

      

    4.23

    条件运算符的优先级过低!!

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

    4.24

    左结合律:代入一个数试试,行不通。

    4.25

    先取反再左移6位,即10001110→1000000

    但是程序运行结果是-7296,这是因为题目最后问的是“值”而不是字符了,所以应该由8位转向32位。

    最后的结果为:1111 1111 1111 1111 1110 0011 1000 0000   值为:-7296

    4.26

    如果机器上int是32位,那就没有影响;如果机器上int是16位,则若设置第27位为1,那么该数的值仍为0。

    4.27

    (a):3

    (b):7

    (c):true

    (d):true

    4.28

    cout << sizeof(int) << endl;
    

    4.29

    第一行输出:10,对数组使用时,返回的是数组的大小;对指针使用是,返回的是指针本身的大小

    第二行输出:1,对指针解引用使用,返回的是指针所指数据的类型(p指向数组的第一个元素)的大小

    4.31

    本节的程序中使用前置版本或者后置版本的效果是一样的,但是后置版本还需要保留改变前的变量并进行返回,这是额外的无任何意义的操作,因此无需使用后置版本。这里前置版本和后置版本的效果是一样的,无需任何改写。

     1 #include <iostream>
     2 #include <vector>
     3 
     4 using std::cout;
     5 using std::endl;
     6 using std::vector;
     7 
     8 
     9 int main()
    10 {
    11     vector<int> ivec(10,0);
    12     cout << "修改前ivec内元素值" << endl;
    13     for (auto a : ivec)
    14         cout << a << " ";
    15     cout << endl;
    16 
    17     vector<int>::size_type cnt = ivec.size();
    18     for (vector<int>::size_type ix = 0; ix != ivec.size(); ix++, cnt--)
    19         ivec[ix] = cnt;
    20 
    21     cout << "修改后ivec内元素值" << endl;
    22     for (auto a : ivec)
    23         cout << a << " ";
    24     cout << endl;
    25 
    26     return 0;
    27 }
    View Code

    4.32

    利用指针和下标操作数组中的元素。

    4.33

    表达式实质为:(someVal ? ++x), (++yx), (--y)

    显然,编译器会报错,因为上面那个冒号没有定义。

    4.34

     (a):bool转换

     (b):ival转换为float,计算完的结果转换为double

     (c):cval转换为int,计算完毕后转换为double

    4.35

    (a):'a'转换为int,计算完后int转换为char

    (b):ival转换为double,ui转换为double,计算完后double转换为float

    (c):ui转换为float,计算完后float转换为double

    (d):ival转换为float,计算完后float转换为double,最后计算完后double转换为char

    4.36

    i *= static_cast<int>(d);

    4.37

    int i; double d; const string *ps; char *pc; void *pv;

    (a):pv = (void*)ps;    // pv = static_cast<void*>(const_cast<string*>(ps));

    (b):i = int(*pc);     // i = static_cast<int>(*pc)

    (c):pv = &d;      // pv = static_cast<void*>(&d)

    (d):pc = (char*)pv;   // pc = static_cast<char*>(pv)

    4.38

    将 j/i 的结果转换为 double,然后赋给slope。

  • 相关阅读:
    【C++注意事项】1 数据类型及类型转换
    【万里征程——Windows App开发】动态磁贴
    背包问题
    Fence Repair
    Saruman's Army
    字典序最小问题——Best Cow Line
    区间调度问题
    硬币问题
    数据库查询优化的一些总结
    关于减少BUG的思考
  • 原文地址:https://www.cnblogs.com/xzxl/p/7628045.html
Copyright © 2011-2022 走看看