std::find适用于无序数据的线性查找,返回元素位置,时间复杂度O(N);std::binary_search要求数据有序,仅判断存在性,时间复杂度O(log N),效率更高。

在C++ STL中,
std::find
std::binary_search
find
binary_search
find
std::find
std::binary_search
<algorithm>
std::find
std::find
[first, last)
val
first
val
last
立即学习“C++免费学习笔记(深入)”;
std::vector
std::list
std::array
last
#include <iostream>
#include <vector>
#include <algorithm> // for std::find
int main() {
std::vector<int> numbers = {10, 20, 30, 40, 20, 50};
// 使用 std::find 查找 30
auto it_found = std::find(numbers.begin(), numbers.end(), 30);
if (it_found != numbers.end()) {
std::cout << "找到 30,位于索引: " << std::distance(numbers.begin(), it_found) << std::endl;
} else {
std::cout << "未找到 30" << std::endl;
}
// 使用 std::find 查找 99 (不存在的元素)
auto it_not_found = std::find(numbers.begin(), numbers.end(), 99);
if (it_not_found != numbers.end()) {
std::cout << "找到 99" << std::endl;
} else {
std::cout << "未找到 99" << std::endl;
}
return 0;
}std::binary_search
std::binary_search
[first, last)
true
false
#include <iostream>
#include <vector>
#include <algorithm> // for std::binary_search, std::sort
int main() {
std::vector<int> sorted_numbers = {10, 20, 30, 40, 50}; // 假设数据已排序
// 如果数据未排序,需要先排序
// std::vector<int> unsorted_numbers = {50, 10, 30, 20, 40};
// std::sort(unsorted_numbers.begin(), unsorted_numbers.end());
// std::cout << "排序后的数据: ";
// for (int n : unsorted_numbers) {
// std::cout << n << " ";
// }
// std::cout << std::endl;
// 使用 std::binary_search 查找 30
if (std::binary_search(sorted_numbers.begin(), sorted_numbers.end(), 30)) {
std::cout << "找到 30" << std::endl;
} else {
std::cout << "未找到 30" << std::endl;
}
// 使用 std::binary_search 查找 99 (不存在的元素)
if (std::binary_search(sorted_numbers.begin(), sorted_numbers.end(), 99)) {
std::cout << "找到 99" << std::endl;
} else {
std::cout << "未找到 99" << std::endl;
}
return 0;
}binary_search
find
这是一个非常实际的问题,在我个人的开发经验中,如果数据量稍大且确定有序,我几乎总是倾向于
binary_search
std::find
而
std::binary_search
举个例子,假设有一个包含100万个元素的
std::vector
std::find
std::binary_search
这种效率上的天壤之别,在处理大数据集时尤为明显。如果你的应用程序对性能有要求,并且数据可以保持有序,那么选择
binary_search
find
binary_search
find
binary_search
binary_search
find
binary_search
STL的查找算法远不止这两个,它提供了一系列工具来应对各种复杂的查找需求。在我看来,掌握这些工具能让你更灵活地处理数据。
std::lower_bound
std::upper_bound
binary_search
std::lower_bound(first, last, val)
val
std::upper_bound(first, last, val)
val
val
std::vector<int> data = {10, 20, 30, 30, 30, 40, 50};
auto low = std::lower_bound(data.begin(), data.end(), 30); // 指向第一个30
auto up = std::upper_bound(data.begin(), data.end(), 30); // 指向第一个40
std::cout << "30出现的次数: " << std::distance(low, up) << std::endl; // 输出3std::equal_range
Easily find JSON paths within JSON objects using our intuitive Json Path Finder
30
lower_bound
upper_bound
std::pair<iterator, iterator>
first
lower_bound
second
upper_bound
std::find_if
std::find_if_not
find
std::find_if(first, last, predicate)
predicate
true
std::find_if_not(first, last, predicate)
predicate
false
std::vector<int> nums = {1, 2, 3, 4, 5, 6};
auto it_even = std::find_if(nums.begin(), nums.end(), [](int n){ return n % 2 == 0; });
if (it_even != nums.end()) {
std::cout << "第一个偶数是: " << *it_even << std::endl; // 输出2
}std::count
std::count_if
std::count(first, last, val)
val
std::count_if(first, last, predicate)
predicate
std::search
std::find_end
std::search
std::find_end
除了这些通用算法,别忘了STL容器自身也提供了高效的查找方法,比如
std::set
std::map
std::unordered_set
std::unordered_map
find
尽管STL算法强大,但在实际使用中,如果不注意一些细节,可能会掉入性能陷阱。我个人在项目中就遇到过不少因为选择不当或理解偏差导致的性能瓶颈。
对未排序数据使用binary_search
std::binary_search
vector
频繁地对大型数据集进行排序后单次查找: 如果你只有一个查找操作,并且数据是无序的,那么先用
std::sort
std::binary_search
std::find
选择错误的容器类型:
std::vector
std::find
std::list
std::find
list
vector
list
binary_search
list
std::set
std::map
find
std::unordered_set
std::unordered_map
自定义比较器的效率: 如果你为
std::sort
std::binary_search
不必要的拷贝: 当查找对象是复杂类型时,确保比较操作是按引用进行的,避免不必要的对象拷贝。例如,如果你的
vector
std::find
operator==
迭代器失效: 虽然不直接是查找算法本身的性能陷阱,但在使用查找算法得到迭代器后,如果对容器进行了修改(如插入、删除),可能会导致迭代器失效,后续使用失效迭代器会引发未定义行为。这是STL编程中一个非常基础但又容易被忽视的问题。
优化建议:
std::set
std::map
std::unordered_set
std::unordered_map
std::lower_bound
lower_bound
upper_bound
equal_range
binary_search
总之,STL提供了丰富的查找工具,关键在于理解它们的工作原理和适用场景,并结合实际需求做出明智的选择。
以上就是C++STL查找算法find和binary_search使用的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号