Built-in types of front and rear + + + +
int a = 0;
int b = a++; //此时b=0,a=1
int c = 0;
int d = ++c; // 此时d = 1, c = 1
In other words, the pre-++ ++ and the only difference that the rear operator returns the value + is added after the results from previous copy or self-imposed
Overload them in the class
class Age
{
public:
Age& operator++() //前置++
{
++i;
return *this;
}
const Age operator++(int) //后置++
{
Age tmp = *this;
++(*this); //利用前置++
return tmp;
}
Age& operator=(int i) //赋值操作
{
this->i = i;
return *this;
}
private:
int i;
};
As can be seen, if only in order to simply add a private variable i, rear ++ to use a tmp save a copy of the class before the self-imposed on the function stack, it will cause additional memory overhead, it is not necessary, except when the to save a copy before the self-imposed demand.
Iterator in the STL self-imposed
vector<int> vi{1,2,3};
for (auto iter = vi.begin(); iter != vi.end(); iter++);
for (auto iter = vi.begin(); iter != vi.end(); ++iter);
This iteration, using the ++ operator, just to get the iterator pointer plus a built-in only, does not require a copy of the iterator before the pointer moves, so use the front ++ will save more memory than the rear ++, BTW, has been tested in VS2019 environment, post-++ compiler is really no way to optimize .