0

Let us take:

x = list<int>::iterator
y = list<int>::const_iterator
z = vector<int>::iterator
t = vector<int>::const_iterator

Is there any difference between:

  1. ++x and x++?
  2. ++y and y++?
  3. ++z and z++?
  4. ++t and t++?
lulalala
  • 17,572
  • 15
  • 110
  • 169
Billie
  • 8,938
  • 12
  • 37
  • 67

1 Answers1

0

++x is more efficient than x++, because x++ creates a temporary object first.

Also, There will be a difference if you use it in an assignment statement as you do for let say an integer

int x = 8;
int y;
y = x++;//y will be 8
y = ++x;//y will be 10

Similarly if you access iterators like this, pre increment will point to the next iterator first and then do other operation. If post increment, then the current will be accessed and then will be incremented.

Saksham
  • 9,037
  • 7
  • 45
  • 73
  • I didn't understand from this answer whether prefix or postfix modifies the original iterator. Your last sentence is unclear. – Tim Kuipers Feb 21 '17 at 16:04