Go back to Richel Bilderbeek's homepage.
Go back to Richel Bilderbeek's C++ page.
operator-- (also called the decrement operator) decreases an integer's value by one. To increase an integer's value by one, use operator++
operator-- has a pre-fix and post-fix form, which are '--i' and 'i--' respectively. The prefix form returns the value of i its new value, the postfix form returns the value of i before its decrement. Prefer '--i' over 'i--' [1].
#include <cassert> |
There are four ways to decrement a value by 1, that make use of different operators and some of temporary copies.
Code | Temporary copy yes/no |
i = i - 1; | yes |
i -= 1; | no |
--i; | yes |
i--; | no |
struct MyInt |
Go back to Richel Bilderbeek's C++ page.
Go back to Richel Bilderbeek's homepage.