473,324 Members | 2,196 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,324 software developers and data experts.

Boost algorithm ll::accumulate question

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
7 2655
* 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
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
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
"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


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
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
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Koen | last post by:
Hi, I am looking for an algorithm that figures out which numbers from a given set add up to another number. I am sure this has been done before, but I have no idea how such a calculation is...
3
by: Zarathustra | last post by:
I'm sure there must be a simple algorithm for giving the number of "ones" that are connected, for example in an array like this: int a = {0}; a = a = a = a = a = a= a = a = 1; with all the rest...
6
by: MLH | last post by:
Consider an array A(5) consisting of integers 1 through 5 in random order. 3,1,5,2,4. To make things easier to read, I'll eliminate the commas. My objective is to sort 31524 to 12345 by moving...
11
by: Dilip | last post by:
Howdy I have code similar to this in my project: Note: BSTR is an abomination conjured up some disturbed person in the COM world. BSTR strings must be allocated/deallocated using the...
2
by: tonokio | last post by:
I wasn't sure if this was the best place to post since there isn't really an algorithm section for programming. I was curious that if you're given a MST tree of G and P is the shortest path between...
5
by: Seth | last post by:
I can't get this thing made for the life of me. I've gone through every step per the Boost website regarding using bjam. Nothing. Can anyone give any advice or are there pre-made boost hpps for...
9
by: Andrew McLean | last post by:
This really an algorithm question more that a Python question, but it would be implemented in Python.... I have a list of strings, A. I want to find a set of strings B such that for any "a in A"...
1
by: AMP | last post by:
Hello, I just started working with Introduction to Algoriths from MIT Press and my goal is to rewrite them in c#. This is just a learning experiance. The following code: INSERTION-SORT(A) for...
4
by: sklett | last post by:
I realize this could be a little off-topic, but there are some great minds on this NG and I hope you can let me slide this time ;0) I'm designing our system to manage what products can fit in...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.