1

I'm new to C++ and was learning about string concepts and was just experimenting around. I have the following code:

int main(){

    string s1="loc";
    string s2="al var";
    
    cout<<s1.append(s2)<<endl<<s1<<endl<<s1+s2;    
}

Running it in Vs-code I get the output as:

local var
local var
local var

What I don't understand is I append s1 with s2 and it prints as "local var". Then I print s1 and get the output "local var" meaning s1 is updated successfully. But when I print s1+s2 I don't know why it still gives the same output as s1 and s2 isn't being concatenated with s1 in the output. I was expecting answer as "local varal var. I want to know about how and why is this happening.

Thanks!

JaMiT
  • 14,422
  • 4
  • 15
  • 31
  • Which C++ standard are you using when you compile? – Retired Ninja Jan 07 '23 at 04:21
  • 3
    In the old days, there was no guarantee of the order in which the pieces were gathered before sending them off to be printed. This was fixed in C++17 – user4581301 Jan 07 '23 at 04:22
  • Sounds kinda insane, huh? People put up with this because it allowed compilers the leeway to generate more efficient programs.. Eventually it ed over enough people or the performance difference was so weak that the rules changed to provide a nice, predicable order. [For this and a few other cases at least](https://en.cppreference.com/w/cpp/language/eval_order). – user4581301 Jan 07 '23 at 04:31
  • @user4581301 Thanks I'm using the C++14 version. I guess updating it to C++17 will fix it. – Aniket Sahu Jan 07 '23 at 05:02

0 Answers0