|
Boost Users : |
Subject: Re: [Boost-users] [phoenix] or [lamdba] Operating on containers of smart pointers.
From: Steven Watanabe (watanabesj_at_[hidden])
Date: 2008-10-23 12:05:34
AMDG
Raindog wrote:
> I ran into a problem while cleaning up some old code. What I wanted to
> do was replace explicit for loops with some stl algorithms or
> BOOST_FOREACH. Below is the original function:
>
> |//in header file:
> ..snip
> typedef std::vector<Loki::SmartPtr<ADTChunk> > chunk_vec_t
> chunk_vec_t chunks;
> ..snip
>
> //in source file:
> uint32_t ADT::get_connectivity_data( std::vector< std::vector<uint8_t>
> > &output )
> {
> output.resize(chunks.size());
> for(chunk_vec_t::iterator it = chunks.begin(); it <
> chunks.end(); ++it)
> {
> uint32_t success =
> (*it)->get_connectivity_data(output[it-chunks.begin()]);
> }
> return TRUE;
> }
I think what you want is a two argument version of for_each.
#include <boost/cstdint.hpp>
#include <boost/shared_ptr.hpp>
#include <boost/lambda/lambda.hpp>
#include <boost/lambda/bind.hpp>
#include <vector>
typedef std::vector<boost::uint8_t> out_t;
struct ADTChunk {
boost::uint32_t get_connectivity_data(out_t& output) {
return(0);
}
};
typedef std::vector<boost::shared_ptr<ADTChunk> > chunk_vec_t;
chunk_vec_t chunks;
template<class Iter1, class Iter2, class F>
F for_each(Iter1 begin1, Iter1 end1, Iter2 begin2, F f) {
for(; begin1 != end1; ++begin1, ++begin2) {
f(*begin1, *begin2);
}
return(f);
}
boost::uint32_t get_connectivity_data(std::vector<out_t> &output)
{
using namespace boost::lambda;
output.resize(chunks.size());
for_each(chunks.begin(), chunks.end(), output.begin(),
bind(&ADTChunk::get_connectivity_data, *_1, _2));
return 1;
}
In Christ,
Steven Watanabe
Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net