#include <vector>
#include <iostream>
#include <string>
using namespace std;
int main()
{
auto str = std::string("XYZ"); // mutable string
const auto& cstr(str); // const ref to it
vector<string> v;
v.push_back(cstr);
cout << v.front() << endl; // XYZ is printed as expected
*const_cast<char*>(&cstr[0])='*'; // this will modify the first element in the VECTOR (is this expected?)
str[1]='#'; //
cout << str << endl; // prints *#Z as expected
cout << cstr << endl; // prints *#Z as expected
cout << v.front() << endl; // Why *YZ is printed, not XYZ and not *#Z ?
return 0;
}
I2luY2x1ZGUgPHZlY3Rvcj4KI2luY2x1ZGUgPGlvc3RyZWFtPgojaW5jbHVkZSA8c3RyaW5nPgp1c2luZyBuYW1lc3BhY2Ugc3RkOwppbnQgbWFpbigpCnsKICAgIGF1dG8gc3RyID0gc3RkOjpzdHJpbmcoIlhZWiIpOyAvLyBtdXRhYmxlIHN0cmluZwogICAgY29uc3QgYXV0byYgY3N0cihzdHIpOyAgICAgICAgIC8vIGNvbnN0IHJlZiB0byBpdAoKICAgIHZlY3RvcjxzdHJpbmc+IHY7CiAgICB2LnB1c2hfYmFjayhjc3RyKTsKCiAgICBjb3V0IDw8IHYuZnJvbnQoKSA8PCBlbmRsOyAgLy8gWFlaIGlzIHByaW50ZWQgYXMgZXhwZWN0ZWQKCiAgICAqY29uc3RfY2FzdDxjaGFyKj4oJmNzdHJbMF0pPScqJzsgLy8gdGhpcyB3aWxsIG1vZGlmeSB0aGUgZmlyc3QgZWxlbWVudCBpbiB0aGUgVkVDVE9SIChpcyB0aGlzIGV4cGVjdGVkPykKICAgIHN0clsxXT0nIyc7ICAvLwoKICAgIGNvdXQgPDwgc3RyIDw8IGVuZGw7ICAvLyBwcmludHMgKiNaIGFzIGV4cGVjdGVkCiAgICBjb3V0IDw8IGNzdHIgPDwgZW5kbDsgLy8gcHJpbnRzICojWiBhcyBleHBlY3RlZAogICAgY291dCA8PCB2LmZyb250KCkgPDwgZW5kbDsgLy8gV2h5ICpZWiBpcyBwcmludGVkLCBub3QgWFlaIGFuZCBub3QgKiNaID8KCiAgICByZXR1cm4gMDsKfQ==