#include<iostream>#include<vector>usingnamespace std;intmain(){// 4.1int v =5+10*20/2;
cout << v << endl;// 4.2
vector<int> vals;for(int m =0; m <10; m+=2){
vals.push_back(m);
cout << m <<" ";}
cout << endl;int a =*vals.begin();int b =*(vals.begin());
cout << a <<" "<< b << endl;
a =*vals.begin()+1;
b =(*vals.begin())+1;
cout << a <<" "<< b << endl;return0;}
4.2 节练习
#include<iostream>usingnamespace std;intmain(){// 4.4 a = 91int a =12/3*4+5*15+24%4/2;int b =((12/3)*4)+(5*15)+((24%4)/2);
cout << a <<" "<< b << endl;// 4.5 -86, -18, 0, -2
a =-30*3+21/5;
b =-30+3*21/5;int c =30/3*21%5;int d =-30/3*21%4;
cout << a <<" "<< b <<" "<< c <<" "<< d << endl;// 4.6
cin >> a;if(a %2==1) cout <<"odd"<< endl;if(a %2==0) cout <<"even"<< endl;// 4.7 内存溢出,存储空间不够用了// int m = 4294967296;return0;}
4.3 节练习
#include<iostream>#include<vector>#include<string>usingnamespace std;intmain(){// 4.8 相等性 > 与 > 或 ?// 4.9 先判断 cp 是否为空指针,再判断 cp 指向的是否是空字符串constchar*p ="Hello World";// 4.10int val =0;while(cin >> val && val !=42)continue;
cout <<"end"<< endl;// 4.11int a,b,c,d;
cin >> a >> b >> c >> d;if(a > b && b > c && c > d) cout <<"correct"<< endl;else cout <<"incorrect"<< endl;// 4.12 i != j < k// i != j 返回一个 bool 数,然后将这个 bool 与 k 相比较return0;}
4.4 节练习
#include<iostream>#include<vector>#include<string>usingnamespace std;intmain(){// 4.13int i;double d;
d = i =3.5;
cout << i <<" "<< d << endl;// 3 3
i = d =3.5;
cout << i <<" "<< d << endl;// 3 3.5// 4.14// if( i = 42) 将42赋值给i,再将i转为bool// 4.15double dval;int ival;int*pi;
dval = ival =*pi =0;// 4.16// if ((p = get()) != 0)// if (i == 1024)return0;}
4.5 节练习
#include<iostream>#include<vector>#include<string>usingnamespace std;intmain(){// 4.17// j = ++i, i += 1 first, then assign j with i// j = i++, assign j with i first, then i += 1// 4.18// 会输出 *v.end(),非法// 4.19
vector<int> vec ={1,2,3,4};int ival =1,*ptr =&ival;// a 想表达的应该是 ptr 不为空指针 且 ptr 指向的 int 不为 0;然后将 ptr 指向的值 + 1if(ptr !=0&&(*ptr)++) cout <<*ptr << endl;// bif(ival++&& ival) cout << ival << endl;// c
vec[ival]<= vec[ival+1];++ival;return0;}
4.6 节练习
#include<iostream>#include<vector>#include<string>usingnamespace std;intmain(){// 4.20
vector<string>v(10,"");auto it = v.begin();// a 合法,先输出 *it,然后it++// b 不合法,string 没有 ++// c 不合法,(*iter).empty()// d 合法// e 合法,++优先级高于 *,将迭代器先++然后再解引// f 合法?return0;}
4.7 节练习
#include<iostream>#include<string>#include<vector>usingnamespace std;intmain(){// 4.21
vector<int> vec;for(int i =0; i !=10;++i) vec.push_back(i);for(auto&i : vec){
i =(i %2==1)? i *2: i;}for(auto i: vec) cout << i <<" ";
cout << endl;// 4.22int grade;while(cin >> grade && grade){
string finalGrade =(grade >90)?"High Pass":(grade >75)?"Low Pass":(grade >60)?"Pass":"Fail";
cout << finalGrade << endl;}// 4.23
string s ="word";
string pl = s +(((s[s.size()-1])=='s')?"":"s");
cout << pl << endl;return0;}
#include<iostream>#include<vector>#include<string>usingnamespace std;intmain(){// 4.31// 前置可以减少内存消耗// 4.32// 4.33int x =1, y =11, someValue =1;
someValue ?++x,++y :--x,--y;
cout << x <<" "<< y << endl;// if someValue: ++x, ++y, --y// if not someValue: ++x, --x, --yreturn0;}
4.11
4.11.1 节练习
#include<iostream>usingnamespace std;intmain(){// 4.34// a float -> bool// b int -> float, float -> double// c char -> int, int -> double// 4.35// a char -> int -> charchar cval ='a'+3;
cout << cval << endl;// b usigned int -> double -> floatunsignedint ui =1;int ival =2;float fval = ui - ival *1.1;
cout << fval << endl;// c unsigned int -> float ->doubledouble dval = ui * fval;
cout << dval << endl;// d double -> charreturn0;}
4.11.3 节练习
#include<iostream>usingnamespace std;intmain(){// 4.36int i =1;double d =2.0;
i *=static_cast<int>(d);
cout << i << endl;// 4.38// 将 j/i 的结果强制转换为double类型return0;}