哪个c++ STL容器提供了' extract_max() ', ' find(element_value) '
Which C++ STL container provides `extract_max()`, `find(element_value)` and `modify(element)` functionality?
我想使用c++ STL容器来实现Prim的算法。我需要extract_max
, find(element)
和modify(element_value)
的功能,但std::priority_queue
只提供extract_max
。有没有其他的容器可以用?显然,我希望所有这些都尽可能快。
编辑:容器还应该提供修改其元素值的功能。
将元素推入std::set<T, std::greater<T>>
,这是一个有序堆。
- 调用
*set::begin()
以在O(1)或O(log(n))上获得最大元素,具体取决于set::begin()
的实现方式。 - 使用
set::find
在O(log(n))内执行搜索 - 要修改一个元素,不幸的是,您必须将其从集合中删除,然后插入修改后的版本。(这也适用于
make_heap
和好友)。可能存在不需要这样做的答案,但是(A)您必须偏执于使用哪些成员进行比较和相等,以及(B)速度差异非常小。所以没有通用的容器是这样工作的。 - 如果元素的顺序不是唯一的,则使用
std::multiset
代替,否则是相同的。
的例子:
#include <iostream>
#include <set>
int main()
{
std::set<int, std::greater<int>> v { 3, 1, 4, 1, 5, 9 };
std::cout << "initially, v: ";
for (auto i : v) std::cout << i << ' ';
std::cout << 'n';
auto largest = *v.begin();
v.erase(v.begin());
std::cout << "largest element: " << largest << 'n';
std::cout << "after removing the largest element, v: ";
for (auto i : v) std::cout << i << ' ';
std::cout << 'n';
}
现场演示
相关文章:
- std::map<struct,struct>::find 找不到匹配项,但是如果我循环通过 begin() 到 end(),我在那里看到匹配项
- OpenMP卸载说'fatal error: could not find accel/nvptx-none/mkoffload'
- <streamsize>C++ 中 numeric_limits::max() 的值
- 黑客级别的Mini-Max Sum
- 'max'匹配'std::function<const int &(const int &, const int &)>'无过载
- 使用.find函数在c++中查找字符和另一个字符之间的大小
- 在 QVector<std::unique_ptr 上使用 std::find<Type>>
- 错误"Could not find Boost"(缺少:上下文标头)
- 关于 std::min, std::max 中的比较运算符的混淆
- /usr/bin/ld: cannot find -lc++
- std::find,返回所有找到的值的替代方法,而不仅仅是存在重复的向量的第一个值
- 找到一种有效的方法,在 2 个巨大的缓冲区上执行 MAX,每字节字节
- 使用 find 解析文件会给出不同文件的奇怪结果
- std::max() 函数与定点实现的比较中的问题
- 使用 CImg 库的 std::min 和 std::max 的编译问题
- 在 ifcondition al中 find() C++ STL 中的 == a.end() 有什么用?
- 在 3ds Max 中更新进度条后,环境和效果 UI 不刷新
- 从自定义类获取对象向量中的 max 元素
- C++ 等同于 Matlab Max(find(array < x) 和 min(find(array > x)
- 从Find Max方法返回一个int值并将其打印在测试文件中