STL中适合高效查找的容器有std::unordered_map、std::unordered_set、std::map、std::set和排序后的std::vector。其中std::unordered_map和std::unordered_set基于哈希表,平均查找时间复杂度为O(1),适用于对查找速度要求高且不关心顺序的场景;std::map和std::set基于红黑树,查找时间复杂度为O(log N),适用于需要有序数据或稳定性能的场景;排序后的std::vector结合二分查找可实现O(log N)查找,适合静态或低频更新的数据集。选择时需权衡数据规模、操作频率、是否有序及自定义类型的哈希或比较支持。

C++标准模板库(STL)通过提供一系列经过高度优化的容器(如
std::vector
std::map
std::unordered_map
std::sort
std::find
std::binary_search
STL 提供了一整套工具来应对各种查找和排序需求。对于排序,
std::sort
std::vector
std::find
std::binary_search
std::lower_bound
std::map
std::unordered_map
在STL中,针对高效查找,我们通常会在
std::vector
std::map
std::set
std::unordered_map
std::unordered_set
std::vector
std::sort
std::binary_search
std::lower_bound
std::upper_bound
立即学习“C++免费学习笔记(深入)”;
std::map
std::set
map
set
std::unordered_map
std::unordered_set
unordered_map
std::sort
std::sort
std::sort
Person
std::sort
Person
自定义比较器可以是:
operator()
我通常倾向于使用Lambda表达式,因为它简洁且可以直接在调用
std::sort
#include <vector>
#include <algorithm>
#include <string>
#include <iostream>
struct Person {
std::string name;
int age;
double height;
};
int main() {
std::vector<Person> people = {
{"Alice", 30, 1.65},
{"Bob", 25, 1.80},
{"Charlie", 30, 1.75},
{"David", 25, 1.70}
};
// 示例1: 按年龄升序排序
// 如果年龄相同,则按姓名升序排序
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
if (a.age != b.age) {
return a.age < b.age;
}
return a.name < b.name;
});
std::cout << "Sorted by age, then name:\n";
for (const auto& p : people) {
std::cout << p.name << ", " << p.age << ", " << p.height << "\n";
}
// 示例2: 按身高降序排序
std::sort(people.begin(), people.end(), [](const Person& a, const Person& b) {
return a.height > b.height; // 注意是 > 实现降序
});
std::cout << "\nSorted by height (descending):\n";
for (const auto& p : people) {
std::cout << p.name << ", " << p.age << ", " << p.height << "\n";
}
return 0;
}通过这种方式,我们可以轻松地根据任何复杂的逻辑来对自定义数据类型进行排序。我记得刚开始学C++的时候,自定义排序函数让我觉得有点神奇,因为它可以把我的“比较规则”直接传给算法,非常灵活。
std::find
std::binary_search
std::find
std::binary_search
std::find
std::find
std::find
std::binary_search
std::binary_search
std::lower_bound
std::upper_bound
何时选用:
std::find
std::binary_search
lower_bound
upper_bound
lower_bound
upper_bound
我经常看到一些新手在处理一个大型数据集时,反复调用
std::find
std::sort
std::binary_search
std::map
std::unordered_map
std::unordered_map
std::map
std::unordered_map
std::map
性能优势:
std::unordered_map
std::map
潜在陷阱: 尽管
unordered_map
unordered_map
map
unordered_map
std::hash
unordered_map
map
std::vector
map
unordered_map
unordered_map
map
unordered_map
何时选用:
unordered_map
map
例如,如果你要存储一个人的ID到其详细信息的映射,并且ID是
int
string
unordered_map
map
#include <iostream>
#include <string>
#include <unordered_map>
#include <map>
// 自定义类型作为键
struct Point {
int x, y;
// 必须提供相等运算符
bool operator==(const Point& other) const {
return x == other.x && y == other.y;
}
};
// 为自定义类型提供哈希函数
// 方式1: 特化std::hash
namespace std {
template <>
struct hash<Point> {
size_t operator()(const Point& p) const {
// 一个简单的哈希组合,实际应用中可能需要更复杂的哈希函数
return hash<int>()(p.x) ^ (hash<int>()(p.y) << 1);
}
};
}
int main() {
std::unordered_map<Point, std::string> umap;
umap[{1, 2}] = "Point A";
umap[{3, 4}] = "Point B";
if (umap.count({1, 2})) {
std::cout << "Found in unordered_map: " << umap[{1, 2}] << std::endl;
}
// std::map 也可以使用 Point 作为键,但 Point 必须定义 operator<
std::map<Point, std::string> m;
// Point 必须有 operator<
// bool operator<(const Point& other) const {
// if (x != other.x) return x < other.x;
// return y < other.y;
// }
// 如果没有,这里会编译错误
return 0;
}这段代码展示了
unordered_map
operator==
std::hash
unordered_map
map
operator<
以上就是C++如何使用STL实现高效查找和排序的详细内容,更多请关注php中文网其它相关文章!
每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。
Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号