I have a pair of iterators returned from a multimap equal_range call. I wish to use these to create a subset in the form of a vector of pairs. Can this be done elegantly please?
The reason I want it as a vector is so I can more easily extract data based on its index (position in the container)
Using the iterator range constructor of std::vector
:
auto p = mul_map.equal_range(...);
std::vector<mul_map_type::value_type> v(p.first, p.second);
For efficiency, it may be worth to only store pointers or iterators in the vector, which can easily be achieved with Boost.Range:
#include <boost/range/counting_range.hpp>
auto p = mul_map.equal_range(...);
auto iters = boost::counting_range(p.first, p.second);
std::vector<mul_map_type::(const_)iterator> v(iters.begin(), iters.end());