C++ C++;:为了简化代码块,有没有办法使用基于值的条件运算符?
比如说:C++ C++;:为了简化代码块,有没有办法使用基于值的条件运算符?,c++,conditional,simplify,C++,Conditional,Simplify,比如说: if(CurrentRotationStage % 2 == 0) { if(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit) < 0.f) { CurrentRotatio
if(CurrentRotationStage % 2 == 0)
{
if(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit) < 0.f)
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
}
else
{
if(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit) > 0.f)
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
}
if(CurrentRotationStage%2==0)
{
如果(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage],0.f,CubicPoint[(CurrentRotationStage+1)%4],0.f,MainMenuWidget->TrumpAngle/RotationLimit)<0.f)
{
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
}
其他的
{
如果(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage],0.f,CubicPoint[(CurrentRotationStage+1)%4],0.f,MainMenuWidget->TrumpAngle/RotationLimit)>0.f)
{
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
}
基本上,如果CurrentRotationStage是偶数,我想在if语句中使用<,如果是奇数,则相反。是否有任何方法可以简化此过程以防止使用多个if/ELSE?此部分应放在一个变量中
(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit)
那么它看起来是这样的:
blah = calculateIt...
if(CurrentRotationStage % 2 == 0 && blah < 0.f) ||
(CurrentRotationStage % 2 != 0 && blah > 0.f){
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
#include <iostream>
#include <algorithm>
#include <functional>
int main() {
std::function<bool(int,int)> op = std::less<int>();
std::cout << "Defaulting to less-than" << std::endl;
int x = 5;
if (x & 1) {
op = std::greater<int>();
std::cout << "Chosing greater-than because number is odd" << std::endl;
}
if (op(x, 4)) {
std::cout << x << " is op(4)" << std::endl;
}
}
blah=calculateIt。。。
如果(CurrentRotationStage%2==0&&blah<0.f)||
(CurrentRotationStage%2!=0&&blah>0.f){
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
此部分应放入变量中
(FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit)
那么它看起来是这样的:
blah = calculateIt...
if(CurrentRotationStage % 2 == 0 && blah < 0.f) ||
(CurrentRotationStage % 2 != 0 && blah > 0.f){
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
#include <iostream>
#include <algorithm>
#include <functional>
int main() {
std::function<bool(int,int)> op = std::less<int>();
std::cout << "Defaulting to less-than" << std::endl;
int x = 5;
if (x & 1) {
op = std::greater<int>();
std::cout << "Chosing greater-than because number is odd" << std::endl;
}
if (op(x, 4)) {
std::cout << x << " is op(4)" << std::endl;
}
}
blah=calculateIt。。。
如果(CurrentRotationStage%2==0&&blah<0.f)||
(CurrentRotationStage%2!=0&&blah>0.f){
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
将比较逻辑拉入公共函数或类中
class RotationChecker
{
public:
explicit RotationChecker(int stage): stage_(stage) {}
bool operator()(float f) const { return stage % 2 == 0 ? f < 0.f : f > 0.f; }
private:
int stage_;
};
RotationChecker checker(CurrentRotationStage);
float value = FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f,
CubicPoint[(CurrentRotationStage + 1) % 4], 0.f,
MainMenuWidget->TrumpAngle / RotationLimit);
if (checker(value))) // or "if (RotationChecker(CurrentRotationStage)(value))"
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
类旋转检查器
{
公众:
显式旋转检查器(int stage):stage_(stage){}
bool操作符()(float f)const{return stage%2==0?f<0.f:f>0.f;}
私人:
int阶段;
};
旋转检查器(CurrentRotationStage);
浮点值=FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage],0.f,
立方点[(CurrentRotationStage+1)%4],0.f,
主菜单Widget->Trump角度/旋转限制);
if(checker(value))//或“if(RotationChecker(CurrentRotationStage)(value))”
{
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
将比较逻辑拉入公共函数或类中
class RotationChecker
{
public:
explicit RotationChecker(int stage): stage_(stage) {}
bool operator()(float f) const { return stage % 2 == 0 ? f < 0.f : f > 0.f; }
private:
int stage_;
};
RotationChecker checker(CurrentRotationStage);
float value = FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f,
CubicPoint[(CurrentRotationStage + 1) % 4], 0.f,
MainMenuWidget->TrumpAngle / RotationLimit);
if (checker(value))) // or "if (RotationChecker(CurrentRotationStage)(value))"
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
类旋转检查器
{
公众:
显式旋转检查器(int stage):stage_(stage){}
bool操作符()(float f)const{return stage%2==0?f<0.f:f>0.f;}
私人:
int阶段;
};
旋转检查器(CurrentRotationStage);
浮点值=FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage],0.f,
立方点[(CurrentRotationStage+1)%4],0.f,
主菜单Widget->Trump角度/旋转限制);
if(checker(value))//或“if(RotationChecker(CurrentRotationStage)(value))”
{
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
一般来说,如果您想要一个可切换的操作符,可以这样做:
blah = calculateIt...
if(CurrentRotationStage % 2 == 0 && blah < 0.f) ||
(CurrentRotationStage % 2 != 0 && blah > 0.f){
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
#include <iostream>
#include <algorithm>
#include <functional>
int main() {
std::function<bool(int,int)> op = std::less<int>();
std::cout << "Defaulting to less-than" << std::endl;
int x = 5;
if (x & 1) {
op = std::greater<int>();
std::cout << "Chosing greater-than because number is odd" << std::endl;
}
if (op(x, 4)) {
std::cout << x << " is op(4)" << std::endl;
}
}
#包括
#包括
#包括
int main(){
std::function op=std::less();
std::cout一般来说,如果您想要一个可切换的操作符,如下所示:
blah = calculateIt...
if(CurrentRotationStage % 2 == 0 && blah < 0.f) ||
(CurrentRotationStage % 2 != 0 && blah > 0.f){
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
#include <iostream>
#include <algorithm>
#include <functional>
int main() {
std::function<bool(int,int)> op = std::less<int>();
std::cout << "Defaulting to less-than" << std::endl;
int x = 5;
if (x & 1) {
op = std::greater<int>();
std::cout << "Chosing greater-than because number is odd" << std::endl;
}
if (op(x, 4)) {
std::cout << x << " is op(4)" << std::endl;
}
}
#包括
#包括
#包括
int main(){
std::function op=std::less();
std::cout我将从现有的几个答案中选择这个方法:
std::function<bool (int, int)> fn = std::less<int>();
if (CurrentRotationStage % 2 == 0)
{
fn = std::greater<int>();
}
float result = FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit);
if (fn(result, 0.f))
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
std::function fn=std::less();
如果(CurrentRotationStage%2==0)
{
fn=标准::更大();
}
float result=FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage],0.f,CubicPoint[(CurrentRotationStage+1)%4],0.f,MainMenuWidget->TrumpAngle/RotationLimit);
如果(fn(结果,0.f))
{
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
我将从现有的几个答案中选择这个方法:
std::function<bool (int, int)> fn = std::less<int>();
if (CurrentRotationStage % 2 == 0)
{
fn = std::greater<int>();
}
float result = FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage], 0.f, CubicPoint[(CurrentRotationStage + 1) % 4], 0.f, MainMenuWidget->TrumpAngle / RotationLimit);
if (fn(result, 0.f))
{
CurrentRotationStage = ++CurrentRotationStage % 4;
MainMenuWidget->TrumpAngle -= RotationLimit;
}
std::function fn=std::less();
如果(CurrentRotationStage%2==0)
{
fn=标准::更大();
}
float result=FMath::CubicInterpDerivative(CubicPoint[CurrentRotationStage],0.f,CubicPoint[(CurrentRotationStage+1)%4],0.f,MainMenuWidget->TrumpAngle/RotationLimit);
如果(fn(结果,0.f))
{
CurrentRotationStage=++CurrentRotationStage%4;
MainMenuWidget->TrumpAngle-=旋转限制;
}
如果cubicinterpdivative==0怎么办?
没关系,什么都不应该发生,导数符号从正变为负,或从负变为正,这取决于阶段。这些都是很好的答案,解决了我不知道/忘记的不同问题,谢谢。如果CubicIntERP导数==0
?这很好,什么都不应该发生,导数符号从正变为负,或从负变为正,这取决于阶段。这些都是很好的答案,解决了我不知道/忘记的不同问题,谢谢。是的,谢谢你提醒我这么做如果我的用例被扩展到有多个不同的用例,我会对带有运算符的条件语句感到好奇,这将形成一个非常大的条件语句。是的,谢谢你提醒我这么做,尽管我很好奇如果我的用例被扩展到有多个不同的用例,我会对带有运算符的条件语句感到好奇ich将生成一个非常大的条件语句。有趣的是,我不知道你可以这样做,谢谢。编译器可以像使用本机运算符一样优化它吗?@ChrisBetti,不,因为你是根据运行时值切换运算符。优化的解决方案类似于ergonaut posted-简化总体l代码。但是当一个人真正想要在不重复代码的情况下选择运行时,他可以得到。有趣的是,我不知道你可以做这样的事情,谢谢。编译器可以像使用本机操作符一样优化它吗?@ChrisBetti,不,因为y