[Cs3] cs44001: range based for loops on multimap

Mikhail Nesterenko mikhail at cs.kent.edu
Mon Sep 28 21:34:17 EDT 2015


> 
>     I have a question about using range based for loops on
> multimaps.  I looked around and it looks like range based for loops
> can be used on any container with a begin and end function to return
> the correct iterators.  I wanted to know if there is any way to
> change the iterators the range based for would use, for instance: I
> want to output all elements in a multimap with the same key, can I
> modify a range based for to use the upper_bound and lower _bound
> function iterators for that key from the multimap instead of begin
> and end, or do i have to use a standard for loop?

range-based-for is rather tightly bound with a container. It is not
executed on an arbitrary range. Why not just use a regular for?

    for(auto it  = mymultimap.lower_bound(key); 
             it != mymultimap.upper_bound(key);
 	     ++it)
	     // do what you need

Alternatively, there is a for_each algorithm that we have not yet
studied.

Thanks,
--
Mikhail


More information about the cs3 mailing list