c ++ set ordered iterating - 迭代集合的结果,按标准排序? [英] c++ set ordered iterating - Are results of iterating over set, ordered by standard?
问题描述
我可以依靠假设(换句话说:它是否由标准保证):
Can I rely on the assumption (in other words: Is it guaranteed by standard?) that :
-
set< T> :: begin()
将指向集合中的最小元素。 - 迭代集合,
; T> :: begin()
直到set< T> :: end()
)将按递增顺序给出元素 - 并确保:
map
?
set<T>::begin()
will point to the smallest element in set?- iterating over set, (from
set<T>::begin()
tillset<T>::end()
) will give elements in increasing order? - and just to be sure: Do these properties hold for keys in
map
?
我是指设置
和映射
从 STL
。
PS我知道邻接运算符<()
并定义自己的比较器。
P.S. I know abut operator<()
and defining own comparators. Please ensure me about what standard says about this ?
推荐答案
是的,你可以依赖定义,这取决于元素的键) bool operator<
或作为模板参数传递给集合(映射)的比较函子。因此,根据所使用的比较器,排序将从最小到最大。
Yes, you can rely on that by definition, depending on the element's (keys) bool operator<
or the comparison functor passed as template parameter to the set (map). So the ordering will be from smallest to largest according to the comparator used.
在C ++ 11标准§23.2.4中,处理关联容器的要求:
In the C++11 standard, §23.2.4, dealing with requirements of Associative Containers:
每个关联容器都在Key和一个排序关系比较中参数化,从而在Key的元素上引入严格的弱排序(25.4)。此外,地图和多重映射将任意类型T与密钥相关联。比较类型的对象称为容器的比较对象。
Each associative container is parameterized on Key and an ordering relation Compare that induces a strict weak ordering (25.4) on elements of Key. In addition, map and multimap associate an arbitrary type T with the Key. The object of type Compare is called the comparison object of a container.
然后,在同一节中:
关联容器的迭代器的基本属性是它们以键的非降序顺序遍历容器
,其中非降序由用于
的比较构造它们。对于任何两个可解引用迭代器i和j,使得从i到j的距离为正,
value_comp(* j,* i)== false
The fundamental property of iterators of associative containers is that they iterate through the containers in the non-descending order of keys where non-descending is defined by the comparison that was used to construct them. For any two dereferenceable iterators i and j such that distance from i to j is positive, value_comp(*j, *i) == false
b $ b
其中 value_comp
是比较函数。
这篇关于c ++ set ordered iterating - 迭代集合的结果,按标准排序?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!