Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to use BOOST_FOREACH with a boost::ptr_map?

Tags:

c++

boost

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;
}
like image 535
Martin Avatar asked Jan 20 '09 14:01

Martin


4 Answers

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.

like image 60
Daniel James Avatar answered Nov 06 '22 23:11

Daniel James


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>.

like image 28
Emile Cormier Avatar answered Nov 06 '22 21:11

Emile Cormier


Save yourself the typing and improve readability by using tuples:

boost::ptr_map<int, T> mymap;
int key;
T * value;
BOOST_FOREACH(boost::tie(key, value), mymap)
{
    ...
}
like image 6
Paul Fultz II Avatar answered Nov 06 '22 21:11

Paul Fultz II


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;
}
like image 1
Michael Kristofik Avatar answered Nov 06 '22 23:11

Michael Kristofik