473,699 Members | 2,628 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How to use Boost bind library with STL accumulate algorithm

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_ce nt
: public binary_function <T2, T1, T2>
{
public:
add_area_per_ce nt() { }

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

};

float getTotalAreaPer Cent(BlockDataL ist& bdl) {
return accumulate (bdl.begin(), bdl.end(), 0.0,
add_area_per_ce nt<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
1 3604

yi*****@gmail.c om wrote:
template< class T1, class T2> class add_area_per_ce nt
: public binary_function <T2, T1, T2>
{
public:
add_area_per_ce nt() { }

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

};

float getTotalAreaPer Cent(BlockDataL ist& bdl) {
return accumulate (bdl.begin(), bdl.end(), 0.0,
add_area_per_ce nt<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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
2935
by: P G | last post by:
I hope this is on topic here. I have a problem compiling a simple example of the use of boost::bind. Please take a look at the program below. /**************************************************************************/ #include <boost/function.hpp> #include <boost/bind.hpp> #include <boost/shared_ptr.hpp>
4
2729
by: Arturo Cuebas | last post by:
The program below contains a compile error. Following the program you will find the typical fix then my idea for a library that facilitates a more elegant fix. #include <boost\bind.hpp> using namespace boost; struct C {
16
2606
by: Jeff Flinn | last post by:
At the risk of raising the OT ire of some here, I'd like to know what might be done to raise the awareness of the boost libraries. I've found boost and it's libraries invaluable in my work for ~5 years now. The first two postings in this group this morning are addressed almost trivially by using libraries from www.boost.org. IMO, the functionality in boost increases the usability of C++ at least as much as the standard library does. ...
2
2264
by: IndyStef | last post by:
I am trying to use boost's bind on a member function, on the VC8 compiler. After using several different attempts, I could not get it to work. Does anybody know what is wrong with the code below? The function that doesn't compile is foo::DoTheStuff. All three variations of the for-each loop won't build.
6
2602
by: Alex Buell | last post by:
I think one of the program snippets in the Boost's Bind library is wrong. I've just been trying to find the error in this program as below, perhaps there's an typo in there somewhere but I can't find it. Perhaps one of you lot might know what the typo is: #include <iostream> #include <string> #include <boost/bind.hpp> class tracer
7
2690
by: Henrique A. Menarin | last post by:
I want to create a function _and so that I could write vector<boolasserts; //... bool b = _and(asserts); and it "anded" all the elements in the vector. I implemented it like this:
1
2791
by: Tigera | last post by:
Greetings, I am trying to learn to use the Boost library, and I've run into a frustrating problem with the line marked here: #include <boost/function.hpp> #include <boost/functional.hpp> #include <boost/bind.hpp> #include <iostream>
0
1755
by: Barry Ding | last post by:
#include <map> #include <utility> #include <algorithm> #include <iostream> #include <list> #include <boost/lambda/lambda.hpp> #include <boost/lambda/bind.hpp> using namespace std;
3
2213
by: Stuart Golodetz | last post by:
Hi, Just wondering why the following code doesn't work: #include <iostream> #include <boost/lambda/lambda.hpp> using namespace boost::lambda; int main()
0
8613
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9032
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8908
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8880
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6532
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5869
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4374
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3054
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2344
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.