使用(递增/递减)运算符而不是变量(+/-)1有什么好处?

时间:2022-09-06 12:35:20

What are the advantages of using (increment/decrement) operators rather than variable(+/-)1?

使用(递增/递减)运算符而不是变量(+/-)1有什么好处?

Are there any advantages other than shortening code?

除了缩短代码之外还有什么优点吗?

6 个解决方案

#1


2  

There is one: Increment/decrement is limited to one. For example using forward/backward iterators:

有一个:增量/减量限制为一个。例如,使用向前/向后迭代器:

// Assume a forward_iterator would support operator +
forward_iterator operattor + (const forward_iterator&, size_type n) {
    switch(n) {
       case 1: // return next forward_iterator
       break;
       default: // throw an exception ?
       break;
   }
}

The operator ++ avoids the trouble described above.

运算符++避免了上述问题。

#2


1  

For example input iterators have no operator +. They have only the increment operator And using increment and decrement operators make code more expressive.

例如,输入迭代器没有operator +。它们只有增量运算符并且使用递增和递减运算符使代码更具表现力。

#3


1  

When talking about builtin types like int and statements without side effects there is not really a difference and it is just cosmetic.

在谈论像int这样的内置类型和没有副作用的语句时,没有什么区别,它只是化妆品。

But in C++ you might have an iterator class/object and for iterators ++x, x++, x+=1 and x=x+1 are not the same.

但是在C ++中你可能有一个迭代器类/对象,而对于迭代器++ x,x ++,x + = 1和x = x + 1则不一样。

  • x=x+1; is similar to tmp=x.operator+(1); x.operator=(tmp); which creates tmp object with the result of the Addition and then copies the tmp object to x
  • X = X + 1;类似于tmp = x.operator +(1); x.operator =(TMP);使用Addition的结果创建tmp对象,然后将tmp对象复制到x

  • x+=1 is similar to x.operator+(1) which modifies x directly
  • x + = 1类似于x.operator +(1),它直接修改x

  • x++ is similar to calling (void)x.operator++() where operator++ makes a temporary copy of x, modifies x and returns the temporary copy which later is discarded
  • x ++类似于调用(void)x.operator ++(),其中operator ++生成x的临时副本,修改x并返回临时副本,稍后将其丢弃

  • ++x is similar to calling x.operator++() where operator++ just modifies x
  • ++ x类似于调用x.operator ++(),其中operator ++只修改x

Please note that the two operator++ given in my description for ++x and x++ are different operators. If you wanted to override them you would write

请注意,我在++ x和x ++的描述中给出的两个operator ++是不同的运算符。如果你想覆盖它们,你会写

X& X::operator++();         //pre increment
const X X::operator++(int); //post increment

Most iterators support ++ and -- whereas not all support +. And as you see prefix ++ has better performance than postfix ++. It is a good idea to generally change to prefix ++.

大多数迭代器支持++和 - 而不是所有支持+。如你所见,前缀++比postfix ++有更好的性能。通常更改为前缀++是个好主意。

#4


0  

Readability as in --departments[i].persons[j].salaries[month]; and side effects as a[++i] = b[++j];

可读性如--departments [i] .persons [j] .salaries [month];副作用为[++ i] = b [++ j];

#5


0  

The compiler will create the same* assembler code on each case.

编译器将在每种情况下创建相同的*汇编代码。

The only benefits will come from code readability.

唯一的好处将来自代码可读性。



(*) have a look at https://*.com/a/12990882/2703169 All of them will call the same assembler instructions (although in different order) and the perfomance will be exactly the same.

(*)看看https://*.com/a/12990882/2703169所有这些都将调用相同的汇编程序指令(尽管顺序不同),性能将完全相同。

#6


0  

The increment/decrement operators ++/-- are effectively a next/previous inbuilt function, and will always increment/decrement by 1, however variable+/-x is not limited to 1, and can take a larger/smaller value. They also allow the implicit control of when the increment/decrement is to happen: i++ will increment i, after it is utilised and ++i will increment it before i is utilised, for example

递增/递减运算符++ / - 实际上是下一个/上一个内置函数,并且总是递增/递减1,但是变量+/- x不限于1,并且可以采用更大/更小的值。它们还允许隐式控制何时发生递增/递减:i ++将在使用之后递增i并且++ i将在使用之前递增它,例如

int i = 1;
 cout << i++ << endl; // outputs 1, sets i to 2 after the cout
 cout << ++i << endl; // outputs 3, sets i to 3 before the cout

you can achieve the same with i+1 and placing it before or after the instruction you wish to utilise i in, however the increment/decrement operators save programmer finger usage, and are more elegant.

您可以使用i + 1实现相同的功能,并将其放在您希望使用的指令之前或之后,但增量/减量运算符可以节省程序员手指的使用,并且更加优雅。

Points of interest

兴趣点

Thought I had best mention this, though I didn't think it within the original scope of answer: you need to also keep in mind what operations take place and how the data is handled when you are utilising each operator type, paying particular attention to:

我以为我最好提一下这个,虽然我不认为它在原来的答案范围内:你还需要记住当你使用每种操作员类型时,操作发生了什么以及如何处理数据,特别注意:

  • value modification (++i as does i+=x): this modifies the value of i by a single increment(++) or a range (x)
  • 值修改(++ i和i + = x):这会通过单个增量(++)或范围(x)修改i的值

  • temporary variable creation: (i++ and i=i+x): this copies the value of I increments it (by 1 for ++ and by x for +x+), modifies the extant variable I to the same value as the temporary variable, returns the temporary variable value and then as the temporary variable falls out of scope for its short life of use, it is destroyed.
  • 临时变量创建:(i ++和i = i + x):这复制I的值递增它(对于++为1,对于+ x +为x),将现有变量I修改为与临时变量相同的值,返回临时变量值,然后当临时变量超出其使用寿命的范围时,它将被销毁。

#1


2  

There is one: Increment/decrement is limited to one. For example using forward/backward iterators:

有一个:增量/减量限制为一个。例如,使用向前/向后迭代器:

// Assume a forward_iterator would support operator +
forward_iterator operattor + (const forward_iterator&, size_type n) {
    switch(n) {
       case 1: // return next forward_iterator
       break;
       default: // throw an exception ?
       break;
   }
}

The operator ++ avoids the trouble described above.

运算符++避免了上述问题。

#2


1  

For example input iterators have no operator +. They have only the increment operator And using increment and decrement operators make code more expressive.

例如,输入迭代器没有operator +。它们只有增量运算符并且使用递增和递减运算符使代码更具表现力。

#3


1  

When talking about builtin types like int and statements without side effects there is not really a difference and it is just cosmetic.

在谈论像int这样的内置类型和没有副作用的语句时,没有什么区别,它只是化妆品。

But in C++ you might have an iterator class/object and for iterators ++x, x++, x+=1 and x=x+1 are not the same.

但是在C ++中你可能有一个迭代器类/对象,而对于迭代器++ x,x ++,x + = 1和x = x + 1则不一样。

  • x=x+1; is similar to tmp=x.operator+(1); x.operator=(tmp); which creates tmp object with the result of the Addition and then copies the tmp object to x
  • X = X + 1;类似于tmp = x.operator +(1); x.operator =(TMP);使用Addition的结果创建tmp对象,然后将tmp对象复制到x

  • x+=1 is similar to x.operator+(1) which modifies x directly
  • x + = 1类似于x.operator +(1),它直接修改x

  • x++ is similar to calling (void)x.operator++() where operator++ makes a temporary copy of x, modifies x and returns the temporary copy which later is discarded
  • x ++类似于调用(void)x.operator ++(),其中operator ++生成x的临时副本,修改x并返回临时副本,稍后将其丢弃

  • ++x is similar to calling x.operator++() where operator++ just modifies x
  • ++ x类似于调用x.operator ++(),其中operator ++只修改x

Please note that the two operator++ given in my description for ++x and x++ are different operators. If you wanted to override them you would write

请注意,我在++ x和x ++的描述中给出的两个operator ++是不同的运算符。如果你想覆盖它们,你会写

X& X::operator++();         //pre increment
const X X::operator++(int); //post increment

Most iterators support ++ and -- whereas not all support +. And as you see prefix ++ has better performance than postfix ++. It is a good idea to generally change to prefix ++.

大多数迭代器支持++和 - 而不是所有支持+。如你所见,前缀++比postfix ++有更好的性能。通常更改为前缀++是个好主意。

#4


0  

Readability as in --departments[i].persons[j].salaries[month]; and side effects as a[++i] = b[++j];

可读性如--departments [i] .persons [j] .salaries [month];副作用为[++ i] = b [++ j];

#5


0  

The compiler will create the same* assembler code on each case.

编译器将在每种情况下创建相同的*汇编代码。

The only benefits will come from code readability.

唯一的好处将来自代码可读性。



(*) have a look at https://*.com/a/12990882/2703169 All of them will call the same assembler instructions (although in different order) and the perfomance will be exactly the same.

(*)看看https://*.com/a/12990882/2703169所有这些都将调用相同的汇编程序指令(尽管顺序不同),性能将完全相同。

#6


0  

The increment/decrement operators ++/-- are effectively a next/previous inbuilt function, and will always increment/decrement by 1, however variable+/-x is not limited to 1, and can take a larger/smaller value. They also allow the implicit control of when the increment/decrement is to happen: i++ will increment i, after it is utilised and ++i will increment it before i is utilised, for example

递增/递减运算符++ / - 实际上是下一个/上一个内置函数,并且总是递增/递减1,但是变量+/- x不限于1,并且可以采用更大/更小的值。它们还允许隐式控制何时发生递增/递减:i ++将在使用之后递增i并且++ i将在使用之前递增它,例如

int i = 1;
 cout << i++ << endl; // outputs 1, sets i to 2 after the cout
 cout << ++i << endl; // outputs 3, sets i to 3 before the cout

you can achieve the same with i+1 and placing it before or after the instruction you wish to utilise i in, however the increment/decrement operators save programmer finger usage, and are more elegant.

您可以使用i + 1实现相同的功能,并将其放在您希望使用的指令之前或之后,但增量/减量运算符可以节省程序员手指的使用,并且更加优雅。

Points of interest

兴趣点

Thought I had best mention this, though I didn't think it within the original scope of answer: you need to also keep in mind what operations take place and how the data is handled when you are utilising each operator type, paying particular attention to:

我以为我最好提一下这个,虽然我不认为它在原来的答案范围内:你还需要记住当你使用每种操作员类型时,操作发生了什么以及如何处理数据,特别注意:

  • value modification (++i as does i+=x): this modifies the value of i by a single increment(++) or a range (x)
  • 值修改(++ i和i + = x):这会通过单个增量(++)或范围(x)修改i的值

  • temporary variable creation: (i++ and i=i+x): this copies the value of I increments it (by 1 for ++ and by x for +x+), modifies the extant variable I to the same value as the temporary variable, returns the temporary variable value and then as the temporary variable falls out of scope for its short life of use, it is destroyed.
  • 临时变量创建:(i ++和i = i + x):这复制I的值递增它(对于++为1,对于+ x +为x),将现有变量I修改为与临时变量相同的值,返回临时变量值,然后当临时变量超出其使用寿命的范围时,它将被销毁。