C++ 中 += 和 =+ 运算符的区别?
Difference between += and =+ operators in C++?
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);
}
};
CPP中肯定没有=+运算符这样的代码
X =+ 2 只是表示 x 等于正数 2。您可以通过发布您看到的 'solution'
来扩展您的问题
+= 表示它将 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.
希望对您有所帮助。
这是差异的示例。:)
#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++ 中看到的那样,它甚至可以重载,例如标准 class 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;
}
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);
}
};
CPP中肯定没有=+运算符这样的代码 X =+ 2 只是表示 x 等于正数 2。您可以通过发布您看到的 'solution'
来扩展您的问题+= 表示它将 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.
希望对您有所帮助。
这是差异的示例。:)
#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++ 中看到的那样,它甚至可以重载,例如标准 class 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;
}