By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
449,016 Members | 1,038 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 449,016 IT Pros & Developers. It's quick & easy.

How to use Boost bind library with STL accumulate algorithm

P: n/a
I have a function in STL which add the 'area'
attribute of a list of Rect.

template< class T1, class T2> class add_area_per_cent
: public binary_function<T2, T1, T2>
{
public:
add_area_per_cent() { }

T2 operator() (T2 initial, T1 element) {
if (element != NULL) {
return initial + element->getAreaPerCent();
} else {
return initial;
}
}

};

float getTotalAreaPerCent(BlockDataList& bdl) {
return accumulate (bdl.begin(), bdl.end(), 0.0,
add_area_per_cent<BlockData*, float>());
}
Can you please tell me how can I reduce the amount of
code and use Boost bind library to achieve the same
thing?

Thank you.

Feb 14 '06 #1
Share this Question
Share on Google+
1 Reply


P: n/a

yi*****@gmail.com wrote:
template< class T1, class T2> class add_area_per_cent
: public binary_function<T2, T1, T2>
{
public:
add_area_per_cent() { }

T2 operator() (T2 initial, T1 element) {
if (element != NULL) {
return initial + element->getAreaPerCent();
} else {
return initial;
}
}

};

float getTotalAreaPerCent(BlockDataList& bdl) {
return accumulate (bdl.begin(), bdl.end(), 0.0,
add_area_per_cent<BlockData*, float>());
}
Can you please tell me how can I reduce the amount of
code and use Boost bind library to achieve the same
thing?


Reduce amount of code:
- Remove ctor, it's not needed.
- Make it a struct, no public: line needed

Boost::bind isn't really a solution.

What would work is a projection iterator. That would be an iterator
wrapper
for a BlockDataList::iterator,with an operator* that returns
m_iter->getAreaPerCent();
In that case, you can simply call the 4-argument form of
std::accumulate.

HTH,
Michiel Salters

Feb 15 '06 #2

This discussion thread is closed

Replies have been disabled for this discussion.