vendredi 17 juillet 2020

std::string difference in output when use c+=expression and c=c+expression

In the below code

#include<iostream>
#include<string>
using namespace std;
int main()
{
string a,c="!";
cin>>a;
int l=a.size();
for(int i=0;i<l;i++)
{   
    c=c+"#"+a[i];                                   
}
cout<<c;
}

If I replace c=c+"#"+a[i] with c+="#"+a[i] i get unexpected output. Output in the second case is !boxboxboxbox irrespective of input on https://www.onlinegdb.com/ . On "dev c++" the output is -

enter image description here

But a += b is equivalent to a = a + b . Then what is the reason for the difference in output?

Aucun commentaire:

Enregistrer un commentaire