std::find用于在序列中查找指定值,返回指向首个匹配元素的迭代器或末尾迭代器;它比手动循环更安全、可读性更强,支持自定义类型需重载operator==,并可通过std::find_if和std::find_if_not实现基于谓词的灵活查找。

std::find
std::find
这是一个简单的例子,展示如何在
std::vector
std::find
#include <iostream>
#include <vector>
#include <algorithm> // 包含 std::find
#include <string>
int main() {
std::vector<int> numbers = {10, 20, 30, 40, 50};
int target1 = 30;
int target2 = 60;
// 查找 target1
auto it1 = std::find(numbers.begin(), numbers.end(), target1);
if (it1 != numbers.end()) {
std::cout << "找到了 " << target1 << ",它的值是 " << *it1 << std::endl;
} else {
std::cout << "没有找到 " << target1 << std::endl;
}
// 查找 target2
auto it2 = std::find(numbers.begin(), numbers.end(), target2);
if (it2 != numbers.end()) {
std::cout << "找到了 " << target2 << std::endl;
} else {
std::cout << "没有找到 " << target2 << std::endl;
}
// std::string 也可以看作字符序列
std::string text = "Hello C++";
char char_to_find = 'C';
auto char_it = std::find(text.begin(), text.end(), char_to_find);
if (char_it != text.end()) {
std::cout << "在字符串中找到了字符 '" << *char_it << "'" << std::endl;
} else {
std::cout << "在字符串中没有找到字符 '" << char_to_find << "'" << std::endl;
}
return 0;
}这里
std::find
立即学习“C++免费学习笔记(深入)”;
std::find
我个人觉得,
std::find
for
std::find
std::find
当你看到
std::find(begin, end, value)
for
std::find
std::find
当你想用
std::find
std::find
Person
Product
operator==
std::find
operator==
std::find
来看一个例子:
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct Person {
std::string name;
int age;
// 重载 operator==,告诉 std::find 两个 Person 对象何时算作相等
bool operator==(const Person& other) const {
// 我个人定义:如果名字和年龄都一样,就认为是同一个人
return name == other.name && age == other.age;
}
// 为了方便打印
void print() const {
std::cout << "Name: " << name << ", Age: " << age << std::endl;
}
};
int main() {
std::vector<Person> people = {
{"Alice", 30},
{"Bob", 25},
{"Charlie", 35},
{"Alice", 28} // 另一个 Alice
};
Person target_person1 = {"Bob", 25};
Person target_person2 = {"David", 40};
Person target_person3 = {"Alice", 30};
// 查找 target_person1
auto it1 = std::find(people.begin(), people.end(), target_person1);
if (it1 != people.end()) {
std::cout << "找到了目标人物1: ";
it1->print();
} else {
std::cout << "没有找到目标人物1。" << std::endl;
}
// 查找 target_person2
auto it2 = std::find(people.begin(), people.end(), target_person2);
if (it2 != people.end()) {
std::cout << "找到了目标人物2: ";
it2->print();
} else {
std::cout << "没有找到目标人物2。" << std::endl;
}
// 查找 target_person3 (会找到第一个匹配的 Alice)
auto it3 = std::find(people.begin(), people.end(), target_person3);
if (it3 != people.end()) {
std::cout << "找到了目标人物3: ";
it3->print();
} else {
std::cout << "没有找到目标人物3。" << std::endl;
}
return 0;
}通过重载
operator==
Person
std::find
std::find_if
std::find_if_not
std::find
std::find_if
std::find_if_not
它们的工作方式与
std::find
bool
std::find_if
std::find_if_not
这其实是函数式编程思想在C++中的一个体现,让我们可以把“查找什么”和“如何查找”分离开来。
#include <iostream>
#include <vector>
#include <algorithm>
#include <string>
struct Product {
std::string name;
double price;
int stock;
void print() const {
std::cout << "Product: " << name << ", Price: " << price << ", Stock: " << stock << std::endl;
}
};
int main() {
std::vector<Product> products = {
{"Laptop", 1200.0, 50},
{"Mouse", 25.0, 200},
{"Keyboard", 75.0, 100},
{"Monitor", 300.0, 30},
{"Webcam", 50.0, 0} // 缺货
};
// 使用 std::find_if 查找价格高于100的产品
// Lambda表达式作为谓词
auto expensive_product_it = std::find_if(products.begin(), products.end(),
[](const Product& p){ return p.price > 100.0; });
if (expensive_product_it != products.end()) {
std::cout << "找到了第一个价格高于100的产品: ";
expensive_product_it->print();
} else {
std::cout << "没有找到价格高于100的产品。" << std::endl;
}
// 使用 std::find_if 查找库存为0的产品 (缺货)
auto out_of_stock_it = std::find_if(products.begin(), products.end(),
[](const Product& p){ return p.stock == 0; });
if (out_of_stock_it != products.end()) {
std::cout << "找到了第一个缺货产品: ";
out_of_stock_it->print();
} else {
std::cout << "所有产品都有库存。" << std::endl;
}
// 使用 std::find_if_not 查找第一个不是“鼠标”的产品
auto not_mouse_it = std::find_if_not(products.begin(), products.end(),
[](const Product& p){ return p.name == "Mouse"; });
if (not_mouse_it != products.end()) {
std::cout << "找到了第一个不是鼠标的产品: ";
not_mouse_it->print();
} else {
std::cout << "列表中只有鼠标。" << std::endl;
}
return 0;
}std::find_if
std::find_if_not
以上就是C++算法find使用 元素查找实现方法的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号