C ++中+ =和= +运算符之间的区别?

时间:2016-02-25 18:01:13

标签: c++ operators

C ++中+ =和= +运算符有什么区别?我已经看到了一个解决方案,通过使用= +运算符来找到二叉树的深度。

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);
}
};

3 个答案:

答案 0 :(得分:9)

+ =表示它会使lValue增加右侧值。

= +表示它会将右侧值(带符号)分配给“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.

希望这有帮助。

答案 1 :(得分:3)

这是差异的一个例子。:)

#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;
}        

程序输出

Hello World!
World!

运算符+=是复合赋值运算符,它确实存在于C ++(以及许多其他语言)中。正如您在C ++中看到的那样,它甚至可以重载,例如标准类std::string

符号=+是两个运算符:赋值运算符=和一元加运算符+。在演示程序中,应用于字符串文字的一元加运算符没有效果。

这是其他奇怪操作符的演示程序。:)

#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; 
} 

答案 2 :(得分:0)

CPP中肯定没有= +运算符这样的代码 X = + 2只表示x等于正2.您可以通过发布&#39;解决方案来扩展您的问题。你看到了

相关问题