What I mean is - we know that the std::map
's elements are sorted according to the keys. So, let's say the keys are integers. If I iterate from std::map::begin()
to std::map::end()
using a for
, does the standard guarantee that I'll iterate consequently through the elements with keys, sorted in ascending order?
Example:
std::map<int, int> map_;
map_[1] = 2;
map_[2] = 3;
map_[3] = 4;
for( std::map<int, int>::iterator iter = map_.begin();
iter != map_.end();
++iter )
{
std::cout << iter->second;
}
Is this guaranteed to print 234
or is it implementation defined?
Real life reason: I have a std::map
with int
keys. In very rare situations, I'd like to iterate through all elements, with key, greater than a concrete int
value. Yep, it sounds like std::vector
would be the better choice, but notice my "very rare situations".
EDIT: I know, that the elements of std::map
are sorted.. no need to point it out (for most of the answers here). I even wrote it in my question.
I was asking about the iterators and the order when I'm iterating through a container. Thanks @Kerrek SB for the answer.
I think there is a confusion in data structures.
In most languages, a
map
is simply an AssociativeContainer: it maps a key to a value. In the "newer" languages, this is generally achieved using a hash map, thus no order is guaranted.In C++, however, this is not so:
std::map
is a sorted associative containerstd::unordered_map
is a hash-table based associative container introduced in C++11So, in order to clarify the guarantees on ordering.
In C++03:
std::set
,std::multiset
,std::map
andstd::multimap
are guaranteed to be ordered according to the keys (and the criterion supplied)std::multiset
andstd::multimap
, the standard does not impose any order guarantee on equivalent elements (ie, those which compare equal)In C++11:
std::set
,std::multiset
,std::map
andstd::multimap
are guaranteed to be ordered according to the keys (and the criterion supplied)std::multiset
andstd::multimap
, the Standard imposes that equivalent elements (those which compare equal) are ordered according to their insertion order (first inserted first)std::unordered_*
containers are, as the name imply, not ordered. Most notably, the order of elements may change when the container is modified (upon insertion/deletion).When the Standard says that elements are ordered in a way, it means that:
I hope this clears up any confusion.
This is guaranteed by Associative container requirements in the C++ standard. E.g. see 23.2.4/10 in C++11:
and 23.2.4/11
Yes,
std::map
is a sorted container, ordered by theKey
with the suppliedComparator
. So it is guaranteed.That is surely possible.
Yes ... the elements in a
std::map
have a strict weak-ordering, meaning that the elements will be composed of a set (i.e., there will be no repeats of keys that are "equal"), and equality is determined by testing on any two keys A and B, that if key A is not less than key B, and B is not less than A, then key A is equal to key B.That being said, you cannot properly sort the elements of a
std::map
if the weak-ordering for that type is ambiguous (in your case, where you are using integers as the key-type, that is not a problem). You must be able to define a operation that defines a total order on the type you are using for the keys in yourstd::map
, otherwise you will only have a partial order for your elements, or poset, which has property where A may not be comparable to B. What will typically happen in this scenario is that you'll be able to insert the key/value pairs, but you may end up with duplicate key/value pairs if you iterate through the entire map, and/or detect "missing" key/value pairs when you attempt to perform astd::map::find()
of a specific key/value pair in the map.Yes, that's guaranteed. Moreover,
*begin()
gives you the smallest and*rbegin()
the largest element, as determined by the comparison operator, and two key valuesa
andb
for which the expression!compare(a,b) && !compare(b,a)
is true are considered equal. The default comparison function isstd::less<K>
.The ordering is not a lucky bonus feature, but rather, it is a fundamental aspect of the data structure, as the ordering is used to determine when two keys are the same (by the above rule) and to perform efficient lookup (essentially a binary search, which has logarithmic complexity in the number of elements).