operator++(int) 与 operator ++ 两者区别详解

测试程序

详细分析见代码注释

#include<iostream>
#include <bitset>
using namespace std;

class Point
{
private:
	int x;
public:
	Point(int x1) :x(x1) {};
	int getx() { return this->x; };
	Point& operator++();//成员函数定义自增
    Point operator++(int n); //后缀可以返回一个const类型的值
	Point& operator--();//成员函数定义自减
	Point operator--(int n);//后缀可以返回一个const类型的值
};

Point& Point::operator++()//++obj
{
	cout << "Point& Point::operator++()    ++obj" << endl;
	x++;
	return *this;   //通过引用返回*this ,也就是返回变化之后的数值
} 
Point Point::operator++(int n)//obj++
{
	cout << "const Point Point::operator++(int n)    obj++" << endl;
	Point temp = *this;
	(this->x)++;
	return temp;   //返回原状态的拷贝
}
Point& Point::operator--()//--obj
{
	cout << "Point& Point::operator--()    --obj" << endl;
	x--;
	return *this;
	//通过引用返回*this ,也就是返回变化之后的数值
}
Point Point::operator--(int n)//obj--
{
	cout << "const Point Point::operator--(int n)    obj--" << endl;
	Point temp = *this;
	x--;
	return temp;
	// 返回原状态的拷贝
}
int main()
{
        
	Point a(1);
	Point b(2);
	cout << "a++" << "\t";
	a++;//隐式调用成员函数operator++(0),后缀表达式

	cout << "++a" << "\t";
	++a;//隐式调用成员函数operator++(),前缀表达式

	cout << "b--" << "\t";
	b--;//隐式调用友元函数operator--(0),后缀表达式

	cout << "--b" << "\t";
	--b;//隐式调用友元函数operator--(),前缀表达式
	
	cout << "*************下面测试++组合形式********************" << endl;
	cout << "a=" << a.getx() << endl;
	cout << "(++a)++" << endl;
    //(a++)++;  //错误调用,(a++)返回的是拷贝对象值,对其++不影响a,没有意义
	(++a)++;    //正确,(++a)返回本身引用
	cout << "a=" << a.getx() << endl;
	cout << "*************下面测试--组合形式********************" << endl;
	cout << "b=" << b.getx() << endl;
	cout << "(--b)--" << endl;
	//(b--)--;  //错误调用,(b--)返回的是拷贝对象值,对其--不影响b,没有意义
	(--b)--;    //正确,(--b)返回本身引用
	cout << "b=" << b.getx() << endl;
}

测试结果如下:
在这里插入图片描述

标准库程序

上面测试程序的写法参考了标准库的程序,下面代码来自stl源码 stl_list.h 中iterator相关的运算符重载

template <class T>
struct __list_node {
  typedef void* void_pointer;
  void_pointer next;
  void_pointer prev;
  T data;
};
typedef __list_iterator<T, T&, T*>             iterator;   //迭代器
typedef __list_iterator<T, Ref, Ptr>           self;


  self& operator++() { 
    node = (link_type)((*node).next);   //node指向下一个节点
    return *this;
  }
  self operator++(int) { 
    self tmp = *this;
    ++*this;    //交给上面的self& operator++()处理
    return tmp;    //返回原状态拷贝
  }
  self& operator--() { 
    node = (link_type)((*node).prev);   //node指向上一个节点
    return *this;
  }
  self operator--(int) { 
    self tmp = *this;   
    --*this;   //交给上面的self& operator++()处理
    return tmp;     //返回原状态拷贝
  }

可以发现对iterator来说,iterator++或者iterator–都有一次构造函数,且实际调用的是++iterator和–iterator来处理,效率低下,
所以我们使用迭代器来说,尽量使用++iterator和–iterator操作。

  • 3
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
`reduce()` 方法是 Java 8 中 Stream API 的一个重要方法,它允许我们通过聚合操作将 Stream 元素合并为单个结果。该方法有两个版本:有参数的 `reduce()` 和无参数的 `reduce()`。下面分别介绍它们。 ### 有参数的 reduce() 有参数的 `reduce()` 方法的签名如下: ``` T reduce(T identity, BinaryOperator<T> accumulator) ``` 其中,`identity` 是一个起始值,`accumulator` 是一个二元操作符,它接受两个参数并返回一个结果。该方法的作用是将 Stream 中的元素与起始值进行聚合,聚合方式由 `accumulator` 决定。 下面是一个示例代码: ```java List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); int sum = numbers.stream().reduce(0, (a, b) -> a + b); System.out.println(sum); // 输出 15 ``` 该示例中,我们使用 `reduce()` 方法将一个包含 5 个整数的 Stream 聚合为一个整数。起始值为 0,二元操作符为加法运算符,计算结果为 1+2+3+4+5=15。 ### 无参数的 reduce() 无参数的 `reduce()` 方法的签名如下: ``` Optional<T> reduce(BinaryOperator<T> accumulator) ``` 该方法没有起始值,返回一个 Optional 对象,其中包含聚合结果。如果 Stream 是空的,该方法返回一个空的 Optional。 下面是一个示例代码: ```java List<Integer> numbers = Arrays.asList(1, 2, 3, 4, 5); Optional<Integer> max = numbers.stream().reduce(Integer::max); System.out.println(max); // 输出 Optional[5] ``` 该示例中,我们使用 `reduce()` 方法将一个包含 5 个整数的 Stream 聚合为一个最大值。由于没有起始值,我们使用 `Integer::max` 作为二元操作符。计算结果为 5。 总之,`reduce()` 方法是 Stream API 中的一个重要方法,它允许我们将 Stream 中的元素聚合为单个结果,并支持有参数和无参数两种形式。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值