0

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)

pingu
  • 8,719
  • 12
  • 50
  • 84

1 Answers1

6

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());
Xeo
  • 129,499
  • 52
  • 291
  • 397
  • Using C++11's `auto` just for brevity here, you can always type out the type. :) – Xeo Oct 12 '12 at 14:08