Topic: Boost algorithm ll::accumulate question
Author: ricecake@gehennom.invalid (Marcus Kwok)
Date: Mon, 29 Jan 2007 21:00:43 GMT Raw View
In comp.lang.c++ Henrique A. Menarin <hamenarin@gmail.com> 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 mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: "Henrique A. Menarin" <hamenarin@gmail.com>
Date: Sat, 27 Jan 2007 21:55:21 CST Raw View
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 mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: alfps@start.no ("Alf P. Steinbach")
Date: Sun, 28 Jan 2007 05:23:38 GMT Raw View
* 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 mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: daniel_t@earthlink.net ("Daniel T.")
Date: Sun, 28 Jan 2007 05:24:07 GMT Raw View
In article <1169906041.610594.260620@k78g2000cwa.googlegroups.com>,
"Henrique A. Menarin" <hamenarin@gmail.com> 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 mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Author: cbarron3@ix.netcom.com (Carl Barron)
Date: Sun, 28 Jan 2007 17:28:31 GMT Raw View
"Daniel T." <daniel_t@earthlink.net> 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 mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]