Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Boost algorithm ll::accumulate question

Reply
Thread Tools

Boost algorithm ll::accumulate question

 
 
Henrique A. Menarin
Guest
Posts: n/a
 
      01-28-2007
I want to create a function _and so that I could write

vector<bool> asserts;
//...
bool b = _and(asserts);

and it "anded" all the elements in the vector. I implemented it like
this:


using namespace boost::lambda;

boost::function(bool (vector<boost>)) _and;
_and = bind(ll::accumulate(),
bind(call_begin(), _1),
bind(call_end(), _1),
true,
protect(_1 && _2));

But there is an error with the lambda function protect(_1 && _2).
Does anyone knows what is wrong in this expression?
This syntax is valid for creating similar functions for adding and
multiplying numbers.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use (E-Mail Removed) ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

 
Reply With Quote
 
 
 
 
Alf P. Steinbach
Guest
Posts: n/a
 
      01-28-2007
* Henrique A. Menarin:
> I want to create a function _and so that I could write
>
> vector<bool> asserts;
> //...
> bool b = _and(asserts);
>
> and it "anded" all the elements in the vector. I implemented it like
> this:
>
>
> using namespace boost::lambda;
>
> boost::function(bool (vector<boost>)) _and;
> _and = bind(ll::accumulate(),
> bind(call_begin(), _1),
> bind(call_end(), _1),
> true,
> protect(_1 && _2));
>
> But there is an error with the lambda function protect(_1 && _2).
> Does anyone knows what is wrong in this expression?
> This syntax is valid for creating similar functions for adding and
> multiplying numbers.


The type 'boost' seems to be undefined.

Why don't you write a simple for-loop? And if for some obscure reason a
loop is out of the question, why don't you just use std::accumulate? Is
this perhaps HOMEWORK?

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use (E-Mail Removed) ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

 
Reply With Quote
 
 
 
 
Daniel T.
Guest
Posts: n/a
 
      01-28-2007
In article <(E-Mail Removed) .com>,
"Henrique A. Menarin" <(E-Mail Removed)> wrote:

> I want to create a function _and so that I could write
>
> vector<bool> asserts;
> //...
> bool b = _and(asserts);
>
> and it "anded" all the elements in the vector. I implemented it like
> this:
>
>
> using namespace boost::lambda;
>
> boost::function(bool (vector<boost>)) _and;
> _and = bind(ll::accumulate(),
> bind(call_begin(), _1),
> bind(call_end(), _1),
> true,
> protect(_1 && _2));
>
> But there is an error with the lambda function protect(_1 && _2).
> Does anyone knows what is wrong in this expression?
> This syntax is valid for creating similar functions for adding and
> multiplying numbers.


Use std::bitset and you can just call "none()". It will return true if
any value in the bitset is true.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use (E-Mail Removed) ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

 
Reply With Quote
 
Alan Johnson
Guest
Posts: n/a
 
      01-28-2007
Alf P. Steinbach wrote:
> * Henrique A. Menarin:
>> I want to create a function _and so that I could write
>>
>> vector<bool> asserts;
>> //...
>> bool b = _and(asserts);
>>
>> and it "anded" all the elements in the vector. I implemented it like
>> this:
>>
>>
>> using namespace boost::lambda;
>>
>> boost::function(bool (vector<boost>)) _and;
>> _and = bind(ll::accumulate(),
>> bind(call_begin(), _1),
>> bind(call_end(), _1),
>> true,
>> protect(_1 && _2));
>>
>> But there is an error with the lambda function protect(_1 && _2).
>> Does anyone knows what is wrong in this expression?
>> This syntax is valid for creating similar functions for adding and
>> multiplying numbers.

>
> The type 'boost' seems to be undefined.
>
> Why don't you write a simple for-loop? And if for some obscure reason a
> loop is out of the question, why don't you just use std::accumulate? Is
> this perhaps HOMEWORK?
>


This doesn't sound at all like homework to me. In my experience with
college CS professors most haven't even heard of the std namespace, let
alone boost.

--
Alan Johnson
 
Reply With Quote
 
Carl Barron
Guest
Posts: n/a
 
      01-28-2007
"Daniel T." <(E-Mail Removed)> wrote:

> > boost::function(bool (vector<boost>)) _and;
> > _and = bind(ll::accumulate(),
> > bind(call_begin(), _1),
> > bind(call_end(), _1),
> > true,
> > protect(_1 && _2));
> >
> > But there is an error with the lambda function protect(_1 && _2).
> > Does anyone knows what is wrong in this expression?
> > This syntax is valid for creating similar functions for adding and
> > multiplying numbers.

>
> Use std::bitset and you can just call "none()". It will return true if
> any value in the bitset is true.
>

Perhaps the vector's size depends on runtime conditions and is not
known at compile time, a needed condition for std::bitset<N>.

seems that return std::find(container.begin(),container.end(),true) !=
container.end(); should or the entire container.

I dont see why all the convolutions with tr1/boost bind,myself.

Another point is the iterators for std::vector<bool> are not strictly
random_access iterators or even forward iterators, and this might cause
a problem with the templates involved. This is a known gotcha....
Test it with deque<bool> that as deque does not have a specialization
for bool that packs data into bits, as vector<bool> does.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use (E-Mail Removed) ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

 
Reply With Quote
 
Henrique A. Menarin
Guest
Posts: n/a
 
      01-29-2007


On Jan 28, 3:28 pm, (E-Mail Removed) (Carl Barron) wrote:
> "Daniel T." <(E-Mail Removed)> wrote:
> > > boost::function(bool (vector<boost>)) _and;
> > > _and = bind(ll::accumulate(),
> > > bind(call_begin(), _1),
> > > bind(call_end(), _1),
> > > true,
> > > protect(_1 && _2));

>
> > > But there is an error with the lambda function protect(_1 && _2).
> > > Does anyone knows what is wrong in this expression?
> > > This syntax is valid for creating similar functions for adding and
> > > multiplying numbers.

>
> > Use std::bitset and you can just call "none()". It will return true if
> > any value in the bitset is true. Perhaps the vector's size depends on runtime conditions and is not

> known at compile time, a needed condition for std::bitset<N>.
>
> seems that return std::find(container.begin(),container.end(),true) !=
> container.end(); should or the entire container.
>
> I dont see why all the convolutions with tr1/boost bind,myself.
>
> Another point is the iterators for std::vector<bool> are not strictly
> random_access iterators or even forward iterators, and this might cause
> a problem with the templates involved. This is a known gotcha....
> Test it with deque<bool> that as deque does not have a specialization
> for bool that packs data into bits, as vector<bool> does.
>
> ---
> [ comp.std.c++ is moderated. To submit articles, try just posting with ]
> [ your news-reader. If that fails, use (E-Mail Removed) ]
> [ --- Please see the FAQ before posting. --- ]
> [ FAQ:http://www.comeaucomputing.com/csc/faq.html ]



It's not homework, but part from an university project.
I'm using boost for defining this function inside a stl algorithm,
like find_if.

Compiled with deque<bool>. Thanks, Daniel.

 
Reply With Quote
 
Marcus Kwok
Guest
Posts: n/a
 
      01-29-2007
In comp.lang.c++ Henrique A. Menarin <(E-Mail Removed)> wrote:
> I want to create a function _and so that I could write
>
> vector<bool> asserts;
> //...
> bool b = _and(asserts);
>
> and it "anded" all the elements in the vector.


It sounds like you got the solution to your problem, but be aware that
you should avoid names that start with an underscore ("_and"). Certain
names that begin with an underscore are reserved for the implementation,
and using them makes the behavior of your program undefined. There are
exceptions to this rule (I think maybe using them as a member of a class
is OK, as long as it doesn't start with an underscore followed by a
capital letter, or contain a double-underscore anywhere in the name...),
but I find these rules hard to remember, so I just don't ever use them
at the beginning of names.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use (E-Mail Removed) ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

 
Reply With Quote
 
Roland Pibinger
Guest
Posts: n/a
 
      01-29-2007
On Sat, 27 Jan 2007 23:29:47 -0800, Alan Johnson wrote:
>In my experience with
>college CS professors most haven't even heard of the std namespace, let
>alone boost.


Why should they? Haven't they got better things to do with their time?
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Boost::any and boost::lambda with std::find_if Misiu C++ 3 01-31-2007 05:46 PM
#include <boost/shared_ptr.hpp> or #include "boost/shared_ptr.hpp"? Colin Caughie C++ 1 08-29-2006 02:19 PM
Problems mixing boost::lambda::bind and boost::shared_ptr.. Toby Bradshaw C++ 6 06-02-2006 04:12 PM
Any Boost Experts out there for Boost.Regex? Richard Latter C++ 2 05-17-2004 03:12 PM
Boost + Python C/API: Mixing python return types with boost return types Steve Knight Python 2 10-10-2003 10:11 AM



Advertisments