0

I am sorry. I should change my question. I have the following code

std::vector<int> v = {10, 20, 30};
auto it = v.begin();

std::cout << *it++ << std::endl;    // 1. no parenthesis

std::cout << *(it++) << std::endl;  // 2. with parenthesis

it++;                               // 3. post-increment first
std::cout << *it << std::endl; 

std::cout << *(it + 1) << std::endl;// 4. increment index

What is the difference between the above 4 statements? Is the parenthesis matter in post increment? All outputs are the same as my expectations except 2(with parenthesis).

Kihwan Kim
  • 13
  • 4
  • In this case think on what the ++ operator returns, not when it is evaluated. – user4581301 Jun 08 '21 at 21:04
  • TL;DR of the dupe: With `*(it++)` you are dereferencing the old value of `it`. With `*(++it)` you would be dereferencing the new value of `it`. – NathanOliver Jun 08 '21 at 21:05
  • There is no difference between `*(it++)` and `*it++`. `(*it)++` *is* different, but would produce the same output in this case. – molbdnilo Jun 08 '21 at 21:09
  • In case you can't solve the specific answers from the duplicate: #1 and #2 mean the same thing. #3 is the same as `std::cout << *++it << std::endl;` (see why?) #4 gives the same value as #3 to `cout`, but doesn't advance `it` to the next element. – aschepler Jun 08 '21 at 22:56

0 Answers0