Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1005605
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T08:19:28+00:00 2026-05-16T08:19:28+00:00

Possible Duplicates: Why use ++i instead of i++ in cases where the value is

  • 0

Possible Duplicates:
Why use ++i instead of i++ in cases where the value is not used anywhere else in the statement?
Incrementing in C++ – When to use x++ or ++x?

i++ vs. ++i  

When is this used in real scenarios?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T08:19:28+00:00Added an answer on May 16, 2026 at 8:19 am

    The obvious is when you want the old value returned, you use post-increment.

    The more subtle things are that pre-increment should really never be slower and could be faster due to the lack of creating a temporary and returning the old value when using post-increment.

    A real scenario for using post-increment in C++ is when erasing from standard containers. For example:

    set<int> ctr;
    ctr.insert(1);
    ctr.insert(10);
    ctr.insert(12);
    ctr.insert(15);
    
    set<int>::iterator it = set.begin();
    
    // Post-increment so the value returned to erase is that of the previous
    // iteration (i.e. begin()), yet the iterator stays valid due to the local
    // iterator being incremented prior to the erase call
    ctr.erase(it++);
    
    // Still valid iterator can be used.
    cout << "Value: " << *it << "\n";  
    

    In response to the compiler optimization, it is true yet I think it’s always important to convey as precisely as possible what you’re trying to accomplish. If you don’t need the returned value from x++, then don’t ask for it. Also, I’m not sure you would always get the same optimization if the type your incrementing is not a simple type. Think iterators that are not just plain pointers. In cases such as this, your mileage may vary with regard to optimization. In short, always pre-increment unless you need the returned (i.e. old value) of the post-increment operator.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Why not use tables for layout in HTML? Under what conditions should
Possible Duplicate: Inadvertent use of = instead of == C++ compilers let you know
Possible Duplicates: Valid use of goto for error management in C? Examples of good
Possible Duplicate: Why use TryCast instead of DirectCast? I want to know about TryCast
Possible Duplicate: When to use an interface instead of an abstract class and vice
Possible Duplicate: When or why would you use a right outer join instead of
Possible Duplicates: Use of var keyword in C# Use of “var” type in variable
Possible Duplicates: Why shouldn't I use Objective C 2.0 accessors in init/dealloc? dealloc, use
Possible Duplicate: Use SVN Revision to label build in CCNET I'm working through the
Possible Duplicate: When to use “strictfp” keyword in java? What is the use of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.