Boost algorithm ll::accumulate question

  • Thread starter Henrique A. Menarin
  • Start date
H

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

Alf P. Steinbach

* 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?
 
D

Daniel T.

"Henrique A. Menarin said:
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.
 
A

Alan Johnson

Alf said:
* Henrique A. Menarin:

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

Carl Barron

Daniel T. said:
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.
 
H

Henrique A. Menarin

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 mailto:[email protected] ]
[ --- 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.
 
M

Marcus Kwok

In comp.lang.c++ Henrique A. Menarin said:
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.
 
R

Roland Pibinger

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?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top