Boost algorithm ll::accumulate question

Discussion in 'C++' started by Henrique A. Menarin, Jan 28, 2007.

  1. 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: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
     
    Henrique A. Menarin, Jan 28, 2007
    #1
    1. Advertising

  2. * 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: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
     
    Alf P. Steinbach, Jan 28, 2007
    #2
    1. Advertising

  3. Henrique A. Menarin

    Daniel T. Guest

    In article <>,
    "Henrique A. Menarin" <> 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: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
     
    Daniel T., Jan 28, 2007
    #3
  4. Henrique A. Menarin

    Alan Johnson Guest

    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
     
    Alan Johnson, Jan 28, 2007
    #4
  5. Henrique A. Menarin

    Carl Barron Guest

    "Daniel T." <> 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: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
     
    Carl Barron, Jan 28, 2007
    #5
  6. On Jan 28, 3:28 pm, (Carl Barron) wrote:
    > "Daniel T." <> 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: ]
    > [ --- 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.
     
    Henrique A. Menarin, Jan 29, 2007
    #6
  7. Henrique A. Menarin

    Marcus Kwok Guest

    In comp.lang.c++ Henrique A. Menarin <> 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: ]
    [ --- Please see the FAQ before posting. --- ]
    [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
     
    Marcus Kwok, Jan 29, 2007
    #7
  8. 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?
     
    Roland Pibinger, Jan 29, 2007
    #8
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Brian K. Michalk

    64 bit add and accumulate with MMX

    Brian K. Michalk, Sep 20, 2003, in forum: Perl
    Replies:
    6
    Views:
    662
    Terje Mathisen
    Sep 20, 2003
  2. Roedy Green

    Accumulate 1.0

    Roedy Green, Jul 17, 2004, in forum: Java
    Replies:
    0
    Views:
    403
    Roedy Green
    Jul 17, 2004
  3. cesco
    Replies:
    5
    Views:
    472
    Maxim Yegorushkin
    Feb 13, 2006
  4. Replies:
    1
    Views:
    1,756
  5. Tom Lynch
    Replies:
    2
    Views:
    2,099
    Tom Lynch
    Oct 12, 2007
Loading...

Share This Page