gpt4 book ai didi

c++ - std::map 的顺序

转载 作者:行者123 更新时间:2023-11-30 00:48:10 33 4
gpt4 key购买 nike

对于 std::map ,我能永远相信吗begin()迭代时根据类型的比较运算符返回具有最小键的元素?

换句话说...

威尔std::map<Key, SomeClass>::iterator smallestKeyIt = someMap.begin();给我 map 中 key 最小的那对?

这是为 std::map 确定的顺序吗?或者我可以以某种方式配置它吗?我的理解是,在执行添加和删除元素等操作时,底层树结构保持有序。

最佳答案

can i always trust begin() to return the element with the smallest key according to comparison operators for the type?

是的。

Is this the ordering that is quaranteed for an std::map or can i configure it somehow?

是的。您可以通过指定比较器来配置比较的行为。 (默认的是 std::less。)

来自 cppreference :

template<
class Key,
class T,
class Compare = std::less<Key>,
class Allocator = std::allocator<std::pair<const Key, T> >
> class map;

std::map is a sorted associative container that contains key-value pairs with unique keys. Keys are sorted by using the comparison function Compare. Search, removal, and insertion operations have logarithmic complexity. Maps are usually implemented as red-black trees.

关于c++ - std::map 的顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32471689/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com