Subtracting map iterators

前端 未结 2 1531
南方客
南方客 2020-12-07 03:16

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

相关标签:
2条回答
  • 2020-12-07 03:55

    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.

    0 讨论(0)
  • 2020-12-07 04:08

    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.

    0 讨论(0)
提交回复
热议问题