Efficiently count number of entries between two std::multimap iterators

前提是你 提交于 2019-12-05 05:26:23
TemplateRex

What you are looking for is so-called heterogeneous comparison lookup that was proposed in N3465. It allows for a different but compatible comparison function in the equal_range member function that is being used to store the Key. In your case, the lookup comparison operator (first tuple member) would be different from but consistent with the tuple lexicographical comparison.

Unfortunately, only a small portion of that paper has been accepted into the draft C++14 Standard, according to this Q&A. However, the N3465 paper's author is also the author of Boost.MultiIndex that has implemented this feature. You can emulate a std::multimap by following the Boost.MultiIndex documentation.

Once you have used the generalized equal_range member function of an adapted boost::multiindex_container, you can then simply do std::distance() on the returned iterators. Complexity would be logarithmic in the container size for the equal_range and linear in the size of the returned iterator range. If you are not interested in the result but only in the count, there is also a generalized count() member function returning that in the same logarithmic + linear time.

You want to use the std::distance method from the Iterator library. The reference is at std::distance. Here is the main reference: Iterator library.

The description reads thus as of March 25, 2014:

template< class InputIt >
typename std::iterator_traits<InputIt>::difference_type
    distance( InputIt first, InputIt last );

Returns the number of elements between first and last.

The behavior is undefined if last is not reachable from first by (possibly repeatedly) incrementing first.

Parameters
first - iterator pointing to the first element
last - iterator pointing to the last element

Type requirements
- InputIt must meet the requirements of InputIterator. The operation is more efficient if InputIt additionally meets the requirements of RandomAccessIterator

Return value
The number of elements between first and last.

Complexity
Linear.
However, if InputIt additionally meets the requirements of RandomAccessIterator, complexity is constant.


Note that the above details are subject to change. The most accurate information will be obtained by navigating to the std::distance reference page directly.

Sample code from the std::distance reference web page:

include <iostream>
#include <iterator>
#include <vector>

int main() 
{
    std::vector<int> v{ 3, 1, 4 };

    auto distance = std::distance(v.begin(), v.end());

    std::cout << distance << '\n';
}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!