0

What is the difference between += and =+ operators in C++? I have seen a solution which finds the depth of the binary tree by using =+ operator.

class Solution { 
public:
int maxDepth(TreeNode* root) {
    int l,r;
    if(root == NULL) {
        return 0;
    }
    else {
        l =+ maxDepth(root->left);
        r =+ maxDepth(root->right);
    }
    if (l>r)
    return (l+1);
    else
    return (r+1);
}
};
asx
  • 41
  • 1
  • 1
  • 10
  • 3
    There is not `=+` operator. I would love to see the example. – NathanOliver Feb 25 '16 at 18:02
  • 5
    To start with, there is no `=+` operator. Can you please tell us where you saw that? Maybe it's the assignment operator followed by unary `+`? – Some programmer dude Feb 25 '16 at 18:02
  • 1
    If you have `x=+1;` that indicates that a value of positive `1` is assigned to `x` – phoxis Feb 25 '16 at 18:06
  • @NathanOliver Thanks for the response. I think the solution must be definitely wrong. Here is the program: class Solution { public: int maxDepth(TreeNode* root) { int l,r; if(root == NULL) { return 0; } else { l =+ maxDepth(root->left); r =+ maxDepth(root->right); } if (l>r) return (l+1); else return (r+1); } }; – asx Feb 25 '16 at 18:06
  • Please ignore my not so clean comment, I am new to the community. – asx Feb 25 '16 at 18:08
  • related: http://stackoverflow.com/questions/1642028/what-is-the-name-of-the-operator – 463035818_is_not_an_ai Feb 25 '16 at 18:13
  • 2
    Looks like a misstype – Slava Feb 25 '16 at 18:21
  • Very early in its history, C had an =+ that was like today's +=. It was gone long before C++ entered the picture though. – Jerry Coffin Feb 25 '16 at 18:32

3 Answers3

10

+= means it will increment the lValue by Right side value.

=+ means it will assign the right side value (with sign) to "lValue"

int a = 5;
a += 1;
cout << a; // Here it will print 6.

a =+ 1;
cout << a; // Here it will print 1 (+1).

a =- 1;
cout << a; // Here it will print -1.

Hope this helps.

Sudipta Kumar Sahoo
  • 1,049
  • 8
  • 16
3

Here is an example of the difference.:)

#include <iostream>

int main()
{
    std::string s1( "Hello " );
    std::string s2( "Hello " );

    s1 += "World!";
    s2 =+ "World!";

    std::cout << s1 << std::endl;
    std::cout << s2 << std::endl;
}        

The program output is

Hello World!
World!

Operator += is the compound assignment operator that indeed exists in C++ (and many other languages). As you can see in C++ it can be even overloaded for example for standard class std::string.

Symbols =+ are two operators: the assignment operator = and unary plus operator +. In the demonstrative program the unary plus operator applied to ths string literal has no effect.

Here is a demonstrative program of other weird operators.:)

#include <iostream> 

int main()  
{ 
    int x = 1; 
    int y = 1; 
    int z = 1; 

    if ( x --- y +++ z --> 0 ) std::cout << z << std::endl; 

    return 0; 
} 
Vlad from Moscow
  • 301,070
  • 26
  • 186
  • 335
0

There is certainly no =+ operator in CPP a code like this X =+ 2 simply means x equals positive 2. You can probably expand your question by posting the 'solution' you saw

Spyke
  • 114
  • 4