c++ - C++ set:根据自定义比较器查找和删除元素
问题描述
在我的程序中,set
有类型的元素pair<char, double>
。我还实现了逻辑,以便set
根据元素的第二个值从最小到最大进行排序:
using pair_item = std::pair<char, double>;
std::set<pair_item, decltype([](auto e1, auto e2){return e1.second < e2.second;})> pq;
set
现在,我想根据元素的第一个值从中删除一个元素:
auto first_is_w = std::lower_bound(
pq.begin(), pq.end(), [w](const auto& p) {
return p.first == w;
}
);
if (first_is_w != pq.end() && first_is_w->first == w) {
pq.erase(first_is_w);
}
不幸的是,我得到了错误:
'const A_star(const std::vector<std::tuple<char, char, double> >&, std::unordered_map<char, double>&, char, char)::<lambda(const auto:13&)>' is not derived from 'const std::optional<_Tp>'
{ return *__it < __val; }
~~~~~~^~~~~~~
我想知道我应该如何修改我的 lambda 函数以正确运行搜索?下面附上完整代码:
#include <iostream>
#include <set>
#include <utility>
using pair_item = std::pair<char, double>;
void printSet(const auto& pq) {
std::cout << "Current pq:" << std::endl;
for (const auto& ele : pq) {
std::cout << "(" << ele.first << ", " << ele.second << "), ";
}
std::cout << std::endl;
}
int main() {
char w = 'A';
std::set<pair_item, decltype([](auto e1, auto e2){return e1.second < e2.second;})> pq;
pq.emplace('A', 30);
pq.emplace('B', 20);
pq.emplace('C', 10);
printSet(pq);
auto first_is_w = std::lower_bound(
pq.begin(), pq.end(), [w](const auto& p) {
return p.first == w;
}
);
if (first_is_w != pq.end() && first_is_w->first == w) {
pq.erase(first_is_w);
}
return 0;
}
解决方案
你的 lambda 很好,但是你使用了错误的算法。lower_bound
需要排序范围和严格的弱排序比较,而对于您要查找的值,您没有这些比较。
您应该使用std::find_if
,这是一个 O(N) 线性搜索。
auto first_is_w = std::find_if(
pq.begin(), pq.end(), [w](const auto& p) {
return p.first == w;
}
);
if (first_is_w != pq.end()) {
pq.erase(first_is_w);
}
推荐阅读
- excel - 无法将 1 添加到当前单元格值
- java - Kafka Stream Punctuator 在重建时访问本地存储数据
- typescript - 找不到在 tsconfig `paths` 中定义的模块
- firebase - 无法从 Firebase 下载默认远程配置
- sql - 将 JSON 转换为基于 Presto 的 SQL 查询
- amazon-web-services - 如何在 Alexa 智能家居技能(Lambda)中收听门铃事件
- macos - 如何在mac中手动关闭tcp close_wait连接
- php - ajax调用后如何更新对象,在构造函数中实例化?
- python - 如何在for循环中打开多个文本文件
- python-3.x - 如何在直方图上获得最佳高斯拟合