I have a program where I have two std::map
iterators say left
and right
respectively. I want to find the number of elements in the ran
you can use policy based data structure
which is compiler specific. More details can be found on this codeforces post The same can be used for set as well.
Is it possible to get an O(1) solution for this?
No. a std::map
has a BidirectionalIterator. A BidirectionalIterator does not support random access and can only be incremented or decremented. That means if you want to move 5 positions forward you have to call ++iterator_name
5 times. If you need random access then you will need to pick a container that supports that like a std::array
or std::vector
.