I am not able to find the bug in this small piece of code. what is wrong with it?
string f,s;
f[0] = 'd';
s.append(f);
cout<<f.length()<<" "<<f<<" "<<f[0]<<endl;
cout<<s.length()<<" "<<s<<" "<<s[0]<<endl;
Output is :
0 d
0 d
Even if I change s.length to s.size, the result is the same. Why is s[0] = 'd' and s.size() = 0;?
Both the strings are empty when you create them, they contain no characters.
f[0]is out of bounds, and accessing any element of an empty container is undefined behaviour, so anything can legally happen.You need to do
Or