优化变量值的重复赋值 为了学习C++中的复合赋值,我创建了下面的代码来演示它们所做的: int b05; int b06 = 13; b05 = 49; b05 += b06; // b05 = b05 + b06 cout << "(+=) compound assignment: " << b05 << endl; b05 = 49; b05 -= b06; // b05 = b05 - b06 cout << "(-=) compound assignment: " << b05 << endl; b05 = 49; b05 *= b06; // b05 = b05 * b06 cout << "(*=) compound assignment: " << b05 << endl; b05 = 49; b05 /= b06; // b05 = b05 / b06 cout << "(/=) compound assignment: " << b05 << endl; b05 = 49; b05 %= b06; // b05 = b05 % b06 cout << "(%=) compound assignment: " << b05 << endl; b05 = 49; b05 >>= b06; // b05 = b05 >> b06 cout << "(>>=) compound assignment: " << b05 << endl; b05 = 49; b05 <<= b06; // b05 = b05 << b06 cout << "(<<=) compound assignment: " << b05 << endl; b05 = 49; b05 &= b06; // b05 = b05 & b06 cout << "(&=) compound assignment: " << b05 << endl; b05 = 49; b05 ^= b06; // b05 = b05 ^ b06 cout << "(^=) compound assignment: " << b05 << endl; b05 = 49; b05 |= b06; // b05 = b05 | b06 cout << "(|=) compound assignment: " << b05 << endl; intb05; int b06=13; b05=49; b05+=b06;//b05=b05+b06 你可能误解了复合手术。它们只影响表达式的左值 #include <iostream> int main() { int b05 = 10; int b06 = 5; b05 |= b06; std::cout << "b05 " << b05 << ", b06 " << b06 << "\n"; }
b06未经修改。您误解了复合操作。它们只影响表达式的左值优化变量值的重复赋值 为了学习C++中的复合赋值,我创建了下面的代码来演示它们所做的: int b05; int b06 = 13; b05 = 49; b05 += b06; // b05 = b05 + b06 cout << "(+=) compound assignment: " << b05 << endl; b05 = 49; b05 -= b06; // b05 = b05 - b06 cout << "(-=) compound assignment: " << b05 << endl; b05 = 49; b05 *= b06; // b05 = b05 * b06 cout << "(*=) compound assignment: " << b05 << endl; b05 = 49; b05 /= b06; // b05 = b05 / b06 cout << "(/=) compound assignment: " << b05 << endl; b05 = 49; b05 %= b06; // b05 = b05 % b06 cout << "(%=) compound assignment: " << b05 << endl; b05 = 49; b05 >>= b06; // b05 = b05 >> b06 cout << "(>>=) compound assignment: " << b05 << endl; b05 = 49; b05 <<= b06; // b05 = b05 << b06 cout << "(<<=) compound assignment: " << b05 << endl; b05 = 49; b05 &= b06; // b05 = b05 & b06 cout << "(&=) compound assignment: " << b05 << endl; b05 = 49; b05 ^= b06; // b05 = b05 ^ b06 cout << "(^=) compound assignment: " << b05 << endl; b05 = 49; b05 |= b06; // b05 = b05 | b06 cout << "(|=) compound assignment: " << b05 << endl; intb05; int b06=13; b05=49; b05+=b06;//b05=b05+b06 你可能误解了复合手术。它们只影响表达式的左值 #include <iostream> int main() { int b05 = 10; int b06 = 5; b05 |= b06; std::cout << "b05 " << b05 << ", b06 " << b06 << "\n"; },c++,performance,variables,repeat,compound-assignment,C++,Performance,Variables,Repeat,Compound Assignment,b06未经修改。您误解了复合操作。它们只影响表达式的左值 #include <iostream> int main() { int b05 = 10; int b06 = 5; b05 |= b06; std::cout << "b05 " << b05 << ", b06 " << b06 << "\n"; } b06未经修改。您可以使用宏执行以下操作: #define compound(op)
#include <iostream>
int main() {
int b05 = 10; int b06 = 5;
b05 |= b06;
std::cout << "b05 " << b05 << ", b06 " << b06 << "\n";
}
b06未经修改。您可以使用宏执行以下操作:
#define compound(op) \
b05 = 49; \
b05 op b06; \
std::cout << "(" << #op << ") compound assignment: " << b05 << std::endl;
这实际上是在编译时进行文本替换。在每个有复合(…)
的地方,它都将被复合宏的文本替换,op
将被括号中提供的任何内容替换(在本例中,是某种运算符)
要查看此操作,请执行
g++-E
,您将看到宏(以及任何包含项)已展开。您可以使用宏执行以下操作:
#define compound(op) \
b05 = 49; \
b05 op b06; \
std::cout << "(" << #op << ") compound assignment: " << b05 << std::endl;
这实际上是在编译时进行文本替换。在每个有复合(…)
的地方,它都将被复合宏的文本替换,op
将被括号中提供的任何内容替换(在本例中,是某种运算符)
若要看到这一点,请执行
g++-E
,您将看到宏(以及任何包含项)已展开。首先,没有特别的理由将变量命名为b05
和b06
,因此让我们使用a
和b
,它看起来不那么难看
基本上,您希望复制一对值(两个整数,49
和13
),并将副本传递给将更改它们的代码
最简单的解决方案是创建两个额外的变量,并将值分配给它们:
int a0 = 49, b0 = 13;
然后,每次你想使用它们时,你都可以复制作业:
a = a0; b = b0;
这将避免重复常量(您只指定一次49
和13
),但是您仍然必须复制复制变量的运算符
任何进一步的改进都不会让您避免此副本,它仍然必须在每次操作之前完成,但您可以通过一些技巧避免某些源代码重复:
(1) 使用struct。这将允许您将两个值封装在一个值中:
struct params {
int a;
int b;
}
params p0;
p0.a = 49;
p0.b = 13;
params p;
...
p = p0;
p.a += p.b; // p.a = p.a + p.b
cout << "(+=) compound assignment: " << p.b << endl;
...
虽然没有明确指定a和b,但每次都会发生复制:来自main
的实际参数a
和b
被复制到函数的形式参数变量中,同时也被命名为a
和b
(3) 通过使用函数引用,可以避免重复代码。以下是每个操作的匿名函数数组示例:
typedef int OperationFn(int, int)
struct OperationInfo {
std::string label;
OperationFn op;
}
OperationInfo operations[9] = {
{ "+=", [](int a, int b) { return a += b } },
{ "-=", [](int a, int b) { return a -= b } },
{ "*=", [](int a, int b) { return a *= b } },
{ "/=", [](int a, int b) { return a /= b } },
{ "%=", [](int a, int b) { return a %= b } },
{ ">>=", [](int a, int b) { return a >>= b } },
{ "<<=", [](int a, int b) { return a <<= b } },
{ "&=", [](int a, int b) { return a &= b } },
{ "^=", [](int a, int b) { return a ^= b } },
}
int main() {
for (int i = 0; i < 9; i++) {
const OperationInfo& oi = operations[i];
cout << "(" << oi.label << ") compound assignment: ";
cout << oi.op(49, 13) << endl;
}
return 0;
}
然后不要到处传递b
,只要使用b
注意:请原谅我的打字错误。我手头没有编译器,因此无法验证此代码。它可能有一些小错误。如果你找到了,请留下评论,我会更新我的帖子。首先,没有特别的理由将变量命名为
b05
和b06
,所以让我们使用a
和b
,它看起来不那么难看
基本上,您希望复制一对值(两个整数,49
和13
),并将副本传递给将更改它们的代码
最简单的解决方案是创建两个额外的变量,并将值分配给它们:
int a0 = 49, b0 = 13;
然后,每次你想使用它们时,你都可以复制作业:
a = a0; b = b0;
这将避免重复常量(您只指定一次49
和13
),但是您仍然必须复制复制变量的运算符
任何进一步的改进都不会让您避免此副本,它仍然必须在每次操作之前完成,但您可以通过一些技巧避免某些源代码重复:
(1) 使用struct。这将允许您将两个值封装在一个值中:
struct params {
int a;
int b;
}
params p0;
p0.a = 49;
p0.b = 13;
params p;
...
p = p0;
p.a += p.b; // p.a = p.a + p.b
cout << "(+=) compound assignment: " << p.b << endl;
...
虽然没有明确指定a和b,但每次都会发生复制:来自main
的实际参数a
和b
被复制到函数的形式参数变量中,同时也被命名为a
和b
(3) 通过使用函数引用,可以避免重复代码。以下是每个操作的匿名函数数组示例:
typedef int OperationFn(int, int)
struct OperationInfo {
std::string label;
OperationFn op;
}
OperationInfo operations[9] = {
{ "+=", [](int a, int b) { return a += b } },
{ "-=", [](int a, int b) { return a -= b } },
{ "*=", [](int a, int b) { return a *= b } },
{ "/=", [](int a, int b) { return a /= b } },
{ "%=", [](int a, int b) { return a %= b } },
{ ">>=", [](int a, int b) { return a >>= b } },
{ "<<=", [](int a, int b) { return a <<= b } },
{ "&=", [](int a, int b) { return a &= b } },
{ "^=", [](int a, int b) { return a ^= b } },
}
int main() {
for (int i = 0; i < 9; i++) {
const OperationInfo& oi = operations[i];
cout << "(" << oi.label << ") compound assignment: ";
cout << oi.op(49, 13) << endl;
}
return 0;
}
然后不要到处传递b
,只要使用b
注意:请原谅我的打字错误。我手头没有编译器,因此无法验证此代码。它可能有一些小错误。如果你找到留言,我会更新我的帖子。也许你可以这样做:
int b05, b06 = 13;
compound(+=)
compound(/=)
// etc...
class Obj
{
public:
Obj& operator+= (const Obj &) { return *this; }
Obj& operator-= (const Obj &) { return *this; }
};
void testFunction(std::function<Obj&(Obj *, const Obj &)> functionToTest)
{
Object object;
Object otherObject;
functionToTest(&object, otherObject);
cout << "(+=) function test: " << object << endl;
}
int main(void)
{
std::function<Obj&(Obj *, const Obj &)> functionToTest(&Obj::operator+=);
testFunction(functionToTest);
std::function<Obj&(Obj *, const Obj &)> functionToTest2(&Obj::operator-=);
testFunction(functionToTest2);
}
类Obj
{
公众:
Obj&运算符+=(常量Obj&){return*this;}
Obj&运算符-=(const Obj&){return*this;}
};
void testFunction(std::function-functionToTest)
{
对象对象;
对象其他对象;
functionToTest(对象和其他对象);
cout也许你可以做这样一个函数:
int b05, b06 = 13;
compound(+=)
compound(/=)
// etc...
class Obj
{
public:
Obj& operator+= (const Obj &) { return *this; }
Obj& operator-= (const Obj &) { return *this; }
};
void testFunction(std::function<Obj&(Obj *, const Obj &)> functionToTest)
{
Object object;
Object otherObject;
functionToTest(&object, otherObject);
cout << "(+=) function test: " << object << endl;
}
int main(void)
{
std::function<Obj&(Obj *, const Obj &)> functionToTest(&Obj::operator+=);
testFunction(functionToTest);
std::function<Obj&(Obj *, const Obj &)> functionToTest2(&Obj::operator-=);
testFunction(functionToTest2);
}
类Obj
{
公众:
Obj&运算符+=(常量Obj&){return*this;}
Obj&运算符-=(const Obj&){return*this;}
};
void testFunction(std::function-functionToTest)
{
对象对象;
对象其他对象;
functionToTest(对象和其他对象);
CUT不必重新分配B06,它没有被复合操作修改。代码看起来奇怪、怪异、怪异。你不必重新分配B06,它不会被复合操作修改。代码看起来怪异、怪异、怪异。请不要推荐宏解决方案。这些都不适合编写C++代码。r将函数内联到宏。函数有类型安全性,宏没有。答案很好。毫无疑问,有人会过来发布10页代码,在没有宏的情况下实现同样的功能,并声称它“更好”,因为它没有使用宏。啊哈,废话,我不是要启动宏flamewar。我不正常