for the Problem 1849C
The SOLUTION I wrote, it is having some out of bounds error in the below code portion.
s += (char)('0' ^ '1' ^ s.back());
s = ("" + (char)('0' ^ '1' ^ s[0])) + s; // this line giving error 'out of bounds' though working fine in LOCAL
n = s.size();
I am not able detect the exact reason.
Same purpose If I try to do like below, its working as expected
// working as expected
s += (char)('0' ^ '1' ^ s.back());
reverse(all(s));
s += (char)('0' ^ '1' ^ s.back());
reverse(all(s));
n = s.size();
Check this out: 247667074
Change I made
Thanks, I included the empty string just to make sure the first portion of what we have is a string, as I wasn't sure char to string concat is available.
still do not get why it was an "out of bound" error, I mainly want to understand the reason to avoid the same kinda mistake in the future.
""
is not actually string, it isconst char *
. And when you do"" + 'a'
, you're actually adding ASCII value of'a'
toconst char *
which is pointer arithmetic, you didn't make a new"a"
.But you have done
"Code" + str
wherestd::string str = "Forces";
before and it worked?It is because
std::string
is a class and it can override+
and+=
operator to concatenatestring
withconst char *
and choose not to do pointer arithmetic.Try running the following code, and you'll get junk output or compiler warning, depending upon flags you are using.
""
isconst char*
. And you summarize it with0
symbol which value is 48. So"" + c
is pointer to 48-th symbol of""