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

  • Home
  • SEARCH
  • 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 8122261
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T05:46:47+00:00 2026-06-06T05:46:47+00:00

The operator ++ should be equivalent to + 1 , so why does the

  • 0

The operator ++ should be equivalent to + 1, so why does the following example produce different results?

#include <iostream>
#include <string>

int main()
{
    int i, n=25;
    std::string s1="a", s2="a", s1p="", s2p="";

    for (i=0;i<=n;i++)
    {
        s1p += s1;
        s1 = s1.at(0) + 1;

        s2p += s2;
        s2 = s2.at(0)++;
    }
    std::cout << "s1p = " << s1p << "\n" << "s2p = " << s2p << "\n";

    return 0;
}

Ouput:
s1p = abcdefghijklmnopqrstuvwxyz
s2p = aaaaaaaaaaaaaaaaaaaaaaaaaa
  • 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-06-06T05:46:48+00:00Added an answer on June 6, 2026 at 5:46 am

    The post-increment returns the value previously held by the variable.

    Think of x++ (post-increment) as

    int y = x;
    x = x+1;
    return y;
    

    and of ++x (pre-increment) as

    x = x+1;
    return x;
    

    To achieve your desired result, you need pre-increment.

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

Sidebar

Related Questions

As far as I know, the infix operator usage in Scala should be equivalent
For what purpose I should use std::get_temporary_buffer ? Standard says the following: Obtains a
I want to create a big std::vector so operator[] should receive long long rather
std::string provides const char* c_str ( ) const which: Get C string equivalent Generates
The instructions are to: The MyString object should overload the following operators: 1)Parenthesis operator
As I've understand, when overloading operator=, the return value should should be a non-const
Possible Duplicate: ++ operator in Scala I want to increment an Int variable in
Assuming following code. There is class MyStream witch has template overloaded operator <<. There
Code example: let foo1 (arr : int[]) = for i = 0 to arr.Length-1
I've been using some semi-iterators to tokenize a std::string, and I've run into a

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.