tags:

views:

1579

answers:

5

How can I use BOOST_FOREACH efficiently (number-of-character/readability-wise) with a boost::ptr_map?

Kristo demonstrated in his answer that it is possible to use BOOST_FOREACH with a ptr_map, but it does not really save me any typing (or makes my code really more readable) than iterating over the ptr_map with an iterator:

typedef boost::ptr_container_detail::ref_pair<int, int* const> IntPair;
BOOST_FOREACH(IntPair p, mymap) {
    int i = p.first;
}

// vs.

boost::ptr_map<int, T>::iterator it;
for (it = mymap.begin(); it != mymap.end(); ++it) {
    // doSomething()
}

The following code is somewhere along the lines what I wish for. It follows the standard way on how to use BOOST_FOREACH with a std::map. Unfortunately this does not compile:

boost::ptr_map<int, T> mymap;
// insert something into mymap
// ...

typedef pair<int, T> IntTpair;
BOOST_FOREACH (IntTpair &p, mymap) {
    int i = p.first;
}
A: 

It should compile without the reference:

BOOST_FOREACH (IntTpair p, mymap)

I think the problem is that maps do not actually store objects as pairs, but as a tree structure with the first element as the key, so BOOST_FOREACH can't get a reference to a pair but it can create a temporary copy of one.

Ferruccio
+1  A: 

This example code compiled for me with g++ 4.1.2:

#include "boost/ptr_container/ptr_map.hpp"
#include "boost/foreach.hpp"

int main()
{
    boost::ptr_map<int, int> mymap;

    typedef boost::ptr_container_detail::ref_pair<int, int* const> IntPair;
    BOOST_FOREACH(IntPair p, mymap)
    {
        int i = p.first;
    }

    return 0;
}
Kristo
Thank you Kristo, this is what I eventually also came up with. However, for this one has to write so much in the typedef, that it is not a real abbreviation over the standard for(ptr_map<int, int>::iterator it = mymap.begin(); it != mymap.end(); ++it) {} anymore...
Martin
I agree. I'm interested to see if there's a shorter (better?) way to write that typedef. You also have to consider your coworkers. How many of them will say, "You wrote a crazy typedef just to let you use BOOST_FOREACH? Just write a normal for-loop and be done." :)
Kristo
+6  A: 

As STL style containers, the pointer containers have a value_type typedef that you can use:

#include <boost/ptr_container/ptr_map.hpp>
#include <boost/foreach.hpp>

int main()
{
    typedef boost::ptr_map<int, int> int_map;
    int_map mymap;

    BOOST_FOREACH(int_map::value_type p, mymap)
    {
    }
}

I find that using a typedef for the container makes the code a lot easier to write.

Also, you should try to avoid using the contents of detail namespaces in boost, it's a boost convention that they contain implementation details.

Daniel James
+1  A: 

I just ran into the same problem today. Unfortunately, Daniel's suggestion will not work with a constant reference to a map. In my case, the ptr_map was a member of a class, and I wanted to loop through it in a const member function. Borrowing Daniel's example, this is what I had to do in my case:

#include "boost/ptr_container/ptr_map.hpp"
#include "boost/foreach.hpp"

int main()
{
    typedef boost::ptr_map<int, int> int_map;
    int_map mymap;
    const int_map& mymap_const_ref(mymap);

    BOOST_FOREACH(int_map::const_iterator::value_type p, mymap_const_ref)
    {
    }
}

It seems that int_map::const_iterator::value_type is equivalent to boost::ptr_container_detail::ref_pair<int, const int* const>.

Emile Cormier
A: 

using ::value_type won't let you const-iterate through the container. I use iterator reference types

typedef boost::ptr_map< myKey, myPtrType > MyMap;
MyMap m;
BOOST_FOREACH( MyMap::iterator::reference it, m )
  do_something( it.second );
BOOST_FOREACH( MyMap::const_iterator::reference it, m )
  do_something_const( it.second );
ubik