std::find_if 完全指南

Search for a command to run...

No comments yet. Be the first to comment.
一、有序对和笛卡尔积 1、有序对(序偶):由两个元素 x 和 y 按照确定顺序排列组成的二元组,记作⟨x, y⟩ 2、笛卡尔积:以 A 中元素为第一元、B 中元素为第二元,构造所有有序对⟨x,y⟩; 由全部这类有序对构成的集合,称为 A 与 B 的笛卡尔积,记作 AXB 例题: 已知 A={a,b}, B={0,1,2},求笛卡尔积 A × B、B × A A × B(前元取自 A,后元取自 B

1、集合的概念 N元子集:含有n个元素的子集叫做N元子集. 例题:已知集合 A={1,2,3},按元素个数对 A 的所有子集分类 0元子集:∅ 1元子集:{1}, {2}, {3} 2 元子集:{1,2}, {1,3}, {2,3} 3 元子集:{1,2,3} 幂集:设 A 为集合,由 A 的全部子集构成的集合称为 A 的幂集,记作P(A). 例题:A={1,2,3},求A的幂集 答案:P

一、基本元件 元件 符号 说明 例子 个体词 a, b, c... 代表具体对象(常元) a:小明 个体变元 x, y, z... 代表任意对象 x 表示论域中任一元素 谓词 P(x), Q(x,y)... 表示性质或关系 P(x):x是学生; L(x,y):x喜欢y 量词 ∀, ∃ 修饰个体范围 ∀x(所有x); ∃x(存在x) 连接词 ¬, ∧, ∨, →, ↔ 命

本质是"逻辑推导游戏",给定几个前提,用固定规则一步步推出结论。套路固定,背下规则就能拿分。 一、核心推理规则公式: 假言推理(MP)A→B, A ⇒ B肯定前件→肯定后件拒取式(MT)A→B, ¬B ⇒ ¬A否定后件→否定前件假言三段论(HS)A→B, B→C ⇒ A→C蕴含传递析取三段论(DS)A∨B, ¬A ⇒ B否定一边得另一边附加律A ⇒ A∨B或上一个随便什么化简律A∧B ⇒ A (或

定义:S是一个联结词集合,若任一个命题公式都可以由s中的联结词表示出来命题公式与之等价,则称S是一个联结词完备集。 也就是说一个连接词集合,能表达出所有真值函数,称为完备集 以下是完备集: S1={¬,∧,∨} —— 否定、合取、析取 S2={¬,∧,∨,→} —— 否定、合取、析取、蕴涵 S3={¬,∧,∨,→,↔} —— 否定、合取、析取、蕴涵、等价 S4={¬,∧} —— 否定、

std::find_if 完全指南std::find_if 是 C++ 标准库中的算法,用于在范围内查找第一个满足特定条件的元素。
template< class InputIt, class UnaryPredicate >
InputIt find_if( InputIt first, InputIt last, UnaryPredicate p );
#include <algorithm>
#include <vector>
#include <iostream>
std::vector<int> numbers = {1, 2, 3, 4, 5, 6};
// 查找第一个大于3的元素
auto it = std::find_if(numbers.begin(), numbers.end(),
[](int x) { return x > 3; });
if (it != numbers.end()) {
std::cout << "找到: " << *it << std::endl; // 输出: 找到: 4
}
| 情况 | 捕获方式 | 示例 |
|---|---|---|
| 不使用外部变量 | [] |
[](int x) { return x > 5; } |
| 使用外部变量(值) | [var] |
[threshold](int x) { return x > threshold; } |
| 使用外部变量(引用) | [&var] |
[&threshold](int x) { return x > threshold; } |
| 使用多个变量 | [var1, &var2] |
[min_val, &max_val](int x) { return x > min_val && x < max_val; } |
int threshold = 10;
std::string name_prefix = "user";
// 无捕获 - 硬编码条件
auto it1 = std::find_if(vec.begin(), vec.end(),
[](int x) { return x % 2 == 0; });
// 值捕获
auto it2 = std::find_if(vec.begin(), vec.end(),
[threshold](int x) { return x > threshold; });
// 引用捕获
auto it3 = std::find_if(names.begin(), names.end(),
[&name_prefix](const std::string& s) {
return s.find(name_prefix) == 0;
});
bool isEven(int x) {
return x % 2 == 0;
}
auto it = std::find_if(vec.begin(), vec.end(), isEven);
struct GreaterThan {
int value;
GreaterThan(int v) : value(v) {}
bool operator()(int x) const { return x > value; }
};
auto it = std::find_if(vec.begin(), vec.end(), GreaterThan(10));
#include <functional>
bool inRange(int x, int min, int max) {
return x >= min && x <= max;
}
auto it = std::find_if(vec.begin(), vec.end(),
std::bind(inRange, std::placeholders::_1, 5, 15));
auto predicate = [threshold = getThreshold()](auto x) {
return x > threshold;
};
auto it = std::find_if(container.begin(), container.end(), predicate);
#include <ranges>
auto it = std::ranges::find_if(container,
[](int x) { return x > 10; });
// 使用 std::find_if(推荐)
auto it = std::find_if(vec.begin(), vec.end(),
[threshold](int x) { return x > threshold; });
// 手写循环(性能相当,但可读性差)
auto it = vec.begin();
for (; it != vec.end(); ++it) {
if (*it > threshold) {
break;
}
}
性能结论:
对于简单条件,两者性能基本相当
std::find_if 可读性更好,减少错误
只在经过性能分析确认瓶颈时才考虑手写循环
// 好:意图明确
auto first_negative = std::find_if(numbers.begin(), numbers.end(),
[](int x) { return x < 0; });
// 不好:手写循环,意图不够清晰
auto first_negative = numbers.begin();
for (; first_negative != numbers.end(); ++first_negative) {
if (*first_negative < 0) break;
}
auto it = std::find_if(vec.begin(), vec.end(), predicate);
if (it != vec.end()) {
// 找到元素,进行处理
process(*it);
} else {
// 未找到元素的处理
handle_not_found();
}
// 对于复杂条件,使用命名 Lambda 提高可读性
auto complex_predicate = [min_val = getMin(),
max_val = getMax(),
&config = getConfig()](const auto& item) {
return item.value >= min_val &&
item.value <= max_val &&
item.type == config.allowed_type;
};
auto result = std::find_if(items.begin(), items.end(), complex_predicate);
int threshold = 10;
// 错误:threshold 未捕获
auto it = std::find_if(vec.begin(), vec.end(),
[](int x) { return x > threshold; }); // 编译错误
解决方案:正确捕获变量
auto it = std::find_if(vec.begin(), vec.end(),
[threshold](int x) { return x > threshold; });
int count = 0;
// 错误:试图在 const Lambda 中修改捕获的变量
auto it = std::find_if(vec.begin(), vec.end(),
[count](int x) mutable {
return x > 0 && ++count == 3;
});
解决方案:使用引用捕获或重新设计
int count = 0;
auto it = std::find_if(vec.begin(), vec.end(),
[&count](int x) {
return x > 0 && ++count == 3;
});
基本规则:使用外部变量时加 [value],否则加 []
首选方案:优先使用 std::find_if 而非手写循环
可读性:使用有意义的 Lambda 表达式提高代码可读性
现代特性:在支持的情况下使用 C++14/17/20 的新特性
性能:在大多数情况下,标准算法性能足够,只在必要时优化
std::find_if 是 C++ 中强大而灵活的工具,正确使用可以写出既高效又易维护的代码。