I'm using std::map
to store a lot of elements (pairs of elements) and I have a "little" doubt. What is more efficient to iterate all elements over my std::map
, iterator
or reverse_iterator
?
相关问题
- Sorting 3 numbers without branching [closed]
- How to compile C++ code in GDB?
- Why does const allow implicit conversion of refere
- thread_local variables initialization
- What uses more memory in c++? An 2 ints or 2 funct
相关文章
- Class layout in C++: Why are members sometimes ord
- How to mock methods return object with deleted cop
- Which is the best way to multiply a large and spar
- C++ default constructor does not initialize pointe
- Selecting only the first few characters in a strin
- What exactly do pointers store? (C++)
- Converting glm::lookat matrix to quaternion and ba
- What is the correct way to declare and use a FILE
Does it really matter? these are the types of the micro optimizations you must try to avoid IMHO. Also, even if the iteration time changes for very large number of elements in the map, the fact that you are trying to iterate through all the elements of such a big map means that most probably you have chosen a wrong data structure.
Unless you have profiled your code and found there to be a significant difference, I just wouldn't be concerned about it.
There will likely be no difference. std::reverse_iterator is just a template shim that translates ++'s to --'s at compile time.
For a vector or other contiguous storage container, a forward iterator might interact very slightly better with the cache than a reverse iterator would (unlikely you'd be able to detect it), but for a tree-based container it won't make any difference at all--there won't be any locality of reference to exploit.
Me think it does not make sense to use a reverse_iterator; it's counter intuitive.
It would make the code harder to understand, someone will look at the code and go "wtf"; and if you need to put a comment to explain why, it probably means it's not good code to start with.
I am wondering the need for iteration. Map holds the key-value pairs and normally you use it for the lookup. If you still need to iterate the map for some reasons ( may be deleting pointers contained inside the map etc) then you can use std::for_each. Forget the micro optimizations and you can try to increase the code readability.
For the record, dereferencing
reverse_iterator
onstd::map
andstd::set
containers is twice as slow as usingiterator
-- with both -O3 gcc 3.4.6 and MSVC on Intel/AMD processors (almost 3x as slow on PPC architectures.) Same holds forconst_reverse_iterator
vs.const_iterator
. This is due to the fact thatreverse_iterator
actually points to the tree node immediately following the tree node to be dereferenced, hence the extra work.std::vector
iterators exhibit a much milder difference (reverse_iterator
is only ~30% slower on PPC, virtually indistinguishable on Intel/AMD.) Incidentally, astd::vector
iterator is about 20x faster than astd::map
orstd::set
iterator.