4.迭代器:++it、it++哪个好,为什么

codemagiciant / 2023-08-03 / 原文

4.迭代器:++it、it++哪个好,为什么

1.前置返回一个引用,后置返回一个对象

// ++i实现代码为:
int& operator++()
{
  *this += 1;
  return *this;

} 

2.前置不会产生临时对象,后置必须产生临时对象,临时对象会导致效率降低

//i++实现代码为:                 
int operator++(int)
{
	int temp = *this;
	++* this;
	return temp;
}