The operator ++ should be equivalent to + 1, so why does the following example produce different results?
#include <iostream>
#include <string>
int main()
{
int i, n=25;
std::string s1="a", s2="a", s1p="", s2p="";
for (i=0;i<=n;i++)
{
s1p += s1;
s1 = s1.at(0) + 1;
s2p += s2;
s2 = s2.at(0)++;
}
std::cout << "s1p = " << s1p << "\n" << "s2p = " << s2p << "\n";
return 0;
}
Ouput:
s1p = abcdefghijklmnopqrstuvwxyz
s2p = aaaaaaaaaaaaaaaaaaaaaaaaaa
The post-increment returns the value previously held by the variable.
Think of
x++(post-increment) asand of
++x(pre-increment) asTo achieve your desired result, you need pre-increment.