练习4.17
前置递增运算符和后置递增运算符的区别:
- 前置递增运算符,先将运算对象加1,然后将改变的对象作为求值结果。
- 后置递增运算符,也会将运算对象加1,但是求值结果是运算对象改变之前的那个值的副本。
练习4.18
int main()
{
string s = "some thing";
auto beg = s.begin();
while (beg != s.end() && !isspace(*beg))
{
*beg = toupper(*++beg);
}
cout << s << endl;
}
此时,先把beg的值加1,(即从第一个字符移到了第二个字符),再解引用,再小写转大写,最后返回改变的对象作为求值结果。即sOME thing。
练习4.19
```cpp
#include <iostream>
using namespace std;
#include <string>
#include<vector>
int main()
{
int a = 10;
int* ptr = &a;
vector<int> vec = { 1,2,3 };
int ival = -1;
if (ptr != 0 && *ptr++) //如果指针的值不为0且ptr指向的对象的值也不为0,条件为真,那么ptr指向下一个对象。
{
cout << ptr << endl;
cout << *ptr << endl;
}
if (ival++ && ival) //ival不为0且不为-1的情况下为真
//ival = 0; //ival = -1;
//&&左边不满足条件 //&&左边满足条件
//ival = ival + 1 = 1; //ival = ival + 1 = 0;
//此时&&右边不满足条件
{
cout << ival << endl; //
}
ival = 0;
if (vec[ival++] <= vec[ival])
// 左侧:vec[ival]=vec[0];
// ival = ival + 1 = 1;
// 右侧: vec[ival] = vec[1];
{
cout << "ok" << endl;
cout << vec[ival] << endl;
}
}