4.5 递增和递减运算符

练习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;
		}
     }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值