简化if判断条件
方法1:
#include <iostream>
#include <vector>
#include <functional>
// 封装参数的结构体
struct ConditionParams {
int facenum;
double zoomRatio;
int iso;
double facelv;
int face_w;
double qualityScore;
int xx;
int yy;
};
// 条件检查函数,使用 std::function
bool checkConditions(const ConditionParams& params) {
std::vector<std::pair<std::function<bool(const ConditionParams&)>, std::string>> conditions = {
{[](const ConditionParams& p) { return p.facenum < p.xx; }, "Face number condition failed."},
{[](const ConditionParams& p) { return p.zoomRatio > p.xx; }, "Zoom ratio condition failed."},
{[](const ConditionParams& p) { return p.iso < p.xx && p.iso > p.yy; }, "ISO condition failed."},
{[](const ConditionParams& p) { return p.facelv > p.xx; }, "Face level condition failed."},
{[](const ConditionParams& p) { return p.face_w > p.xx; }, "Face width condition failed."},
{[](const ConditionParams& p) { return p.qualityScore > p.xx; }, "Quality score condition failed."}
};
// 逐个检查条件
for (const auto& [checker, errorMessage] : conditions) {
if (!checker(params)) {
std::cerr << errorMessage << std::endl;
return false;
}
}
return true;
}
int main() {
// 输入的参数
ConditionParams params = {5, 2.0, 400, 1.5, 60, 0.95, 10, 100};
// 检查条件
if (checkConditions(params)) {
std::cout << "All conditions met." << std::endl;
} else {
std::cout << "Some conditions failed." << std::endl;
}
return 0;
}
std::function
:使用std::function<bool(const ConditionParams&)>
取代了函数指针,使得 lambda 表达式更加灵活。ConditionParams
结构体:将所有条件判断的输入参数封装在ConditionParams
结构体中,使得代码简洁且易于扩展。- 简洁的条件检查流程:通过
for
循环遍历所有条件并逐个检查,每个条件不满足时打印对应的错误信息,便于调试。
方法2:
- 将每个条件提取到独立的函数中:这样每个条件的逻辑更加清晰。
- 提供清晰的日志或调试信息:当某个条件未通过时,可以打印出相关的错误或状态信息。
- 使用可读性更好的结构:比如,链式调用或结构体方式,使条件检查更具语义化。
#include <iostream>
bool checkFaceNum(int facenum, int threshold) {
if (facenum < threshold) {
std::cout << "Check failed: facenum < " << threshold << std::endl;
return false;
}
return true;
}
bool checkZoomRatio(float zoomRatio, float threshold) {
if (zoomRatio <= threshold) {
std::cout << "Check failed: zoomRatio <= " << threshold << std::endl;
return false;
}
return true;
}
bool checkIsoRange(int iso, int minThreshold, int maxThreshold) {
if (iso < minThreshold || iso > maxThreshold) {
std::cout << "Check failed: iso out of range [" << minThreshold << ", " << maxThreshold << "]" << std::endl;
return false;
}
return true;
}
bool checkFaceLevel(float facelv, float threshold) {
if (facelv <= threshold) {
std::cout << "Check failed: facelv <= " << threshold << std::endl;
return false;
}
return true;
}
bool checkFaceWidth(float face_w, float threshold) {
if (face_w <= threshold) {
std::cout << "Check failed: face_w <= " << threshold << std::endl;
return false;
}
return true;
}
bool checkQualityScore(float qualityScore, float threshold) {
if (qualityScore <= threshold) {
std::cout << "Check failed: qualityScore <= " << threshold << std::endl;
return false;
}
return true;
}
bool allConditionsMet(int facenum, float zoomRatio, int iso, float facelv, float face_w, float qualityScore) {
return checkFaceNum(facenum, 10) && // 假设阈值为 10
checkZoomRatio(zoomRatio, 1.5) && // 假设阈值为 1.5
checkIsoRange(iso, 100, 800) && // 假设iso范围为100-800
checkFaceLevel(facelv, 0.8) && // 假设facelv阈值为 0.8
checkFaceWidth(face_w, 50) && // 假设face_w阈值为 50
checkQualityScore(qualityScore, 0.9); // 假设质量分数阈值为 0.9
}
int main() {
int facenum = 9;
float zoomRatio = 2.0;
int iso = 400;
float facelv = 1.0;
float face_w = 55.0;
float qualityScore = 0.95;
if (allConditionsMet(facenum, zoomRatio, iso, facelv, face_w, qualityScore)) {
std::cout << "All conditions met, proceeding..." << std::endl;
} else {
std::cout << "Conditions not met, please check the logs for details." << std::endl;
}
return 0;
}
方法3:
如果条件很多,可以使用结构体封装输入参数,并通过链式方法实现条件检查。
#include <iostream>
struct ConditionChecker {
int facenum;
float zoomRatio;
int iso;
float facelv;
float face_w;
float qualityScore;
bool checkFaceNum(int threshold) {
if (facenum < threshold) {
std::cout << "Check failed: facenum < " << threshold << std::endl;
return false;
}
return true;
}
bool checkZoomRatio(float threshold) {
if (zoomRatio <= threshold) {
std::cout << "Check failed: zoomRatio <= " << threshold << std::endl;
return false;
}
return true;
}
bool checkIsoRange(int minThreshold, int maxThreshold) {
if (iso < minThreshold || iso > maxThreshold) {
std::cout << "Check failed: iso out of range [" << minThreshold << ", " << maxThreshold << "]" << std::endl;
return false;
}
return true;
}
bool checkFaceLevel(float threshold) {
if (facelv <= threshold) {
std::cout << "Check failed: facelv <= " << threshold << std::endl;
return false;
}
return true;
}
bool checkFaceWidth(float threshold) {
if (face_w <= threshold) {
std::cout << "Check failed: face_w <= " << threshold << std::endl;
return false;
}
return true;
}
bool checkQualityScore(float threshold) {
if (qualityScore <= threshold) {
std::cout << "Check failed: qualityScore <= " << threshold << std::endl;
return false;
}
return true;
}
// 链式条件检查
bool allConditionsMet() {
return checkFaceNum(10) &&
checkZoomRatio(1.5) &&
checkIsoRange(100, 800) &&
checkFaceLevel(0.8) &&
checkFaceWidth(50) &&
checkQualityScore(0.9);
}
};
int main() {
ConditionChecker checker = {9, 2.0, 400, 1.0, 55.0, 0.95};
if (checker.allConditionsMet()) {
std::cout << "All conditions met, proceeding..." << std::endl;
} else {
std::cout << "Conditions not met, please check the logs for details." << std::endl;
}
return 0;
}