Difference between std::strings + and += operators?

Hey guys,

so I noticed the + and += operators give much different results, I have written a printBinary function which I studied from an online source, when I use the + operator to add to the prefix the function does what it's supposed to do, that is print all the binary numbers in a specified range.

1
2
3
4
5
6
7
8
9
10
11
  void printBinary(int dec,string prefix = ""){

    if(dec == 0){

        cout << prefix << endl;
    }else{

        printBinary(dec-1,prefix + "0");
        printBinary(dec-1,prefix + "1");
    }
}


here is the output when I call printBinary(3);

 
000
001
010
011
100
101
110
111


but when I use the += operator I get a completely different result

1
2
3
4
5
6
7
8
9
10
11
12

void printBinary(int dec,string prefix = ""){

    if(dec == 0){

        cout << prefix << endl;
    }else{

        printBinary(dec-1,prefix += "0");
        printBinary(dec-1,prefix += "1");
    }
}




000
0001
0010
00101
0100
01001
01010
010101



thanks
Do you realize that the first snippet doesn't modify prefix, and that the second snippet does?

+ is evaluated and discarded.
+= modifies prefix.

if prefix is "0"
cout prefix + "0" prints "00" but prefix is still "0", there is no modification.
prefix+= "0" changes prefix to "00" forever.
1) operator+() and operator+=() are different operator, so they are supposed to provide different result.

2) In this code, when you call printBinary() the second time, the value of the local prefix is unaffected by the previous call:
1
2
printBinary(dec-1,prefix += "0");
printBinary(dec-1,prefix += "1");


3) Instead here, at the second call, the local prefix has already been modified by the previous invocation (because += assigns a new value):
1
2
printBinary(dec-1,prefix += "0");
printBinary(dec-1,prefix += "1");


4) To understand that behaviour, you should keep in mind those are recursive functions.
The second call does not start until the entire chain of ‘first’ calls is ended.
At that point, in the two versions of this function, prefix holds a different value.

Ok, I really can’t explain it in English :-( but if you try to ‘cout’ it, it’s self explanatory:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
#include <iostream>
#include <string>


void printBinary_1(int dec, std::string prefix = "");
void printBinary_2(int dec, std::string prefix = "");


int main()
{
    printBinary_1(3);
    std::cout << "- - -\n";
    printBinary_2(3);
    std::cout << '\n';
}


void printBinary_1(int dec, std::string prefix)
{
    if (dec == 0){
        std::cout << prefix << '\n';
    } else {
        std::cout << "_1: prefix: " << prefix
                  << "; passing prefix + \"0\".\n";
        printBinary_1(dec-1, prefix + "0");
        std::cout << "_1: prefix: " << prefix
                  << "; passing prefix + \"1\".\n";
        printBinary_1(dec-1, prefix + "1");
    }
}

void printBinary_2(int dec, std::string prefix)
{
    if (dec == 0){
        std::cout << prefix << '\n';
    } else {
        std::cout << "_2: prefix: " << prefix
                  << "; passing prefix += \"0\".\n";
        printBinary_2(dec-1, prefix += "0");
        std::cout << "_2: prefix: " << prefix
                  << "; passing prefix += \"1\".\n";
        // prefix here has already been modified:
        printBinary_2(dec-1, prefix += "1");
    }
}


Output:
_1: prefix: ; passing prefix + "0".
_1: prefix: 0; passing prefix + "0".
_1: prefix: 00; passing prefix + "0".
000
_1: prefix: 00; passing prefix + "1".
001
_1: prefix: 0; passing prefix + "1".
_1: prefix: 01; passing prefix + "0".
010
_1: prefix: 01; passing prefix + "1".
011
_1: prefix: ; passing prefix + "1".
_1: prefix: 1; passing prefix + "0".
_1: prefix: 10; passing prefix + "0".
100
_1: prefix: 10; passing prefix + "1".
101
_1: prefix: 1; passing prefix + "1".
_1: prefix: 11; passing prefix + "0".
110
_1: prefix: 11; passing prefix + "1".
111
- - -
_2: prefix: ; passing prefix += "0".
_2: prefix: 0; passing prefix += "0".
_2: prefix: 00; passing prefix += "0".
000
_2: prefix: 000; passing prefix += "1".
0001
_2: prefix: 00; passing prefix += "1".
_2: prefix: 001; passing prefix += "0".
0010
_2: prefix: 0010; passing prefix += "1".
00101
_2: prefix: 0; passing prefix += "1".
_2: prefix: 01; passing prefix += "0".
_2: prefix: 010; passing prefix += "0".
0100
_2: prefix: 0100; passing prefix += "1".
01001
_2: prefix: 010; passing prefix += "1".
_2: prefix: 0101; passing prefix += "0".
01010
_2: prefix: 01010; passing prefix += "1".
010101

thanks guys makes sense, although the + and += operators are different I thought they would essentially provide the same function when working on strings.
I thought they would essentially provide the same function when working on strings.

The two operators work differently no matter what the data type:

1
2
3
4
5
6
7
8
9
10
11
12
#include <iostream>

int main()
{
   int aVar { 15 };

   std::cout << (aVar + 5) << ' ';
   std::cout << aVar << '\n';

   std::cout << (aVar += 5) << ' ';
   std::cout << aVar << '\n';
}

20 15
20 20
Topic archived. No new replies allowed.