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

Boost algorithm ll::accumulate question

P: n/a
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:
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 mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Jan 28 '07 #1
Share this Question
Share on Google+
7 Replies


P: n/a
* Henrique A. Menarin:
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:
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 mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Jan 28 '07 #2

P: n/a
In article <11**********************@k78g2000cwa.googlegroups .com>,
"Henrique A. Menarin" <ha*******@gmail.comwrote:
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:
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 mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Jan 28 '07 #3

P: n/a
Alf P. Steinbach wrote:
* Henrique A. Menarin:
>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:
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
Jan 28 '07 #4

P: n/a
"Daniel T." <da******@earthlink.netwrote:
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<boolare 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<boolthat as deque does not have a specialization
for bool that packs data into bits, as vector<booldoes.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Jan 28 '07 #5

P: n/a


On Jan 28, 3:28 pm, cbarr...@ix.netcom.com (Carl Barron) wrote:
"Daniel T." <danie...@earthlink.netwrote:
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<boolare 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<boolthat as deque does not have a specialization
for bool that packs data into bits, as vector<booldoes.

---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-...@ncar.ucar.edu ]
[ --- 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.

Jan 29 '07 #6

P: n/a
In comp.lang.c++ Henrique A. Menarin <ha*******@gmail.comwrote:
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.
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 mailto:st*****@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

Jan 29 '07 #7

P: n/a
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?
Jan 29 '07 #8

This discussion thread is closed

Replies have been disabled for this discussion.