<algorithm> transform modification

Discussion in 'C++' started by Steffen Brinkmann, Sep 21, 2003.

  1. Hi!

    I tried to modify the transform algorithm in a way that it doesn't
    take iterators, but a reference to a container class and a value,
    because Mostly I need to do an operation of a container and a single
    number (e.g. multiply all the values in a vector by 3 or so).
    So, this is my intent:

    #ifndef ALGORITHM_EXT_HH
    #define ALGORITHM_EXT_HH

    #include<iterator>

    namespace std
    {
    template<typename _Tp, template<typename> class _Container,
    typename _Tpval, typename _BinaryOperation>
    _Container<_Tp>&
    transform(_Container<_Tp>& __cont, const _Tpval& __val,
    _BinaryOperation __binary_op)
    {
    _Container<_Tp>::iterator __iter=__cont.begin();
    _Container<_Tp>::iterator __end=__cont.end();
    for ( ; __iter != __end; ++__iter)
    *__iter = __binary_op(*__iter, __val);
    return __cont;
    }

    } //namespace std

    #endif //ALGORITHM_EXT_HH

    The error message I get from g++ (GCC) 3.3 20030226 (prerelease) (SuSE
    Linux) is:


    linux@earth:~/> g++ mod2pnm.cc -o mod2pnm
    In file included from mod2pnm.cc:16:
    algorithm_ext.hh: In function `_Container<_Tp>&
    std::transform(_Container<_Tp>&, const _Tpval&, _BinaryOperation)':
    algorithm_ext.hh:32: error: parse error before `=' token
    algorithm_ext.hh:33: error: parse error before `=' token
    algorithm_ext.hh: In function `_Container<_Tp>&
    std::transform(_Container<_Tp>&, const _Tpval&, _BinaryOperation)
    [with _Tp
    = double, _Container = std::vector, _Tpval = double,
    _BinaryOperation =
    std::minus<double>]':
    mod2pnm.cc:144: instantiated from here
    algorithm_ext.hh:34: error: `__end' undeclared (first use this
    function)
    algorithm_ext.hh:34: error: (Each undeclared identifier is reported
    only once
    for each function it appears in.)
    algorithm_ext.hh:34: error: `__iter' undeclared (first use this
    function)


    Any ideas, what's wrong? I tried all night long!

    Thanks, Steffen
     
    Steffen Brinkmann, Sep 21, 2003
    #1
    1. Advertising

  2. Steffen Brinkmann

    WW Guest

    Steffen Brinkmann wrote:
    > #ifndef ALGORITHM_EXT_HH
    > #define ALGORITHM_EXT_HH
    >
    > #include<iterator>
    >
    > namespace std
    > {


    You are not allowed to place anything into the std namespace!

    > template<typename _Tp, template<typename> class _Container,


    Standard container templates take many more arguments.

    > typename _Tpval, typename _BinaryOperation>
    > _Container<_Tp>&
    > transform(_Container<_Tp>& __cont, const _Tpval& __val,
    > _BinaryOperation __binary_op)
    > {
    > _Container<_Tp>::iterator __iter=__cont.begin();

    typename _Container<_Tp>::iterator __iter=__cont.begin();

    > _Container<_Tp>::iterator __end=__cont.end();

    Ditto

    > for ( ; __iter != __end; ++__iter)
    > *__iter = __binary_op(*__iter, __val);
    > return __cont;
    > }
    >
    > } //namespace std
    >
    > #endif //ALGORITHM_EXT_HH
    >
    > The error message I get from g++ (GCC) 3.3 20030226 (prerelease) (SuSE
    > Linux) is:

    [SNIP]

    Seems that 3.3 has two phase name lookup implemented or at least it refuses
    to guess what is a type anymore.

    --
    WW aka Attila
     
    WW, Sep 21, 2003
    #2
    1. Advertising

  3. Steffen Brinkmann wrote:
    > I tried to modify the transform algorithm in a way that it doesn't
    > take iterators, but a reference to a container class and a value,
    > because Mostly I need to do an operation of a container and a single
    > number (e.g. multiply all the values in a vector by 3 or so).


    In addition to WW's good comments, I thought I might add:

    You might try out std::bind2nd in <functional> and stick with the
    std::transform:

    std::transform(v.begin(), v.end(), v.begin(),
    std::bind2nd(std::multiplies<int>(), 3));

    If you get into more complicated operations, boost::bind might provide
    an answer ( www.boost.org ). boost::bind may eventually be
    standardized. It has been voted into the first library technical report
    which indicates an official interest in this library. The above
    transform translates into bind with:

    std::transform(v.begin(), v.end(), v.begin(),
    boost::bind(std::multiplies<int>(), _1, 3));

    -Howard
     
    Howard Hinnant, Sep 21, 2003
    #3
  4. "WW" <> wrote in message
    news:bkipak$5q2$...
    > [...]
    > You are not allowed to place anything into the std
    > namespace!
    > [...]


    Except specializations of std::less<>, and perhaps a few other
    things.

    Dave
     
    David B. Held, Sep 21, 2003
    #4
  5. Steffen Brinkmann

    Jerry Coffin Guest

    In article <bkipak$5q2$>,
    says...

    [ ... ]

    > You are not allowed to place anything into the std namespace!


    Not so -- you are specifically allowed to add (partial or complete)
    specializations of standard library templates to namespace std. See $
    17.4.3.1/1 for the exact requirements.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 21, 2003
    #5
  6. Steffen Brinkmann

    WW Guest

    David B. Held wrote:
    > "WW" <> wrote in message
    > news:bkipak$5q2$...
    >> [...]
    >> You are not allowed to place anything into the std
    >> namespace!
    >> [...]

    >
    > Except specializations of std::less<>, and perhaps a few other
    > things.


    Yeah. But they have to be specializations, not new things. And I prefer to
    mentioned them for those, who know they can ask this question. :)

    --
    WW aka Attila
     
    WW, Sep 21, 2003
    #6
  7. Steffen Brinkmann

    WW Guest

    Jerry Coffin wrote:
    > In article <bkipak$5q2$>,
    > says...
    >
    > [ ... ]
    >
    >> You are not allowed to place anything into the std namespace!

    >
    > Not so -- you are specifically allowed to add (partial or complete)
    > specializations of standard library templates to namespace std. See $
    > 17.4.3.1/1 for the exact requirements.


    You are not placing them into the standard namespace. They are there and
    you are allowed to specialize them. I prefer to put it this way because I
    have met people (hearing there is exception) kept putting declarations of
    new things there.

    --
    WW aka Attila
     
    WW, Sep 21, 2003
    #7
  8. WW wrote in news:bkjlf4$a3g$:

    > David B. Held wrote:
    >> "WW" <> wrote in message
    >> news:bkipak$5q2$...
    >>> [...]
    >>> You are not allowed to place anything into the std
    >>> namespace!
    >>> [...]

    >>
    >> Except specializations of std::less<>, and perhaps a few other
    >> things.

    >
    > Yeah. But they have to be specializations, not new things. And I
    > prefer to mentioned them for those, who know they can ask this
    > question. :)
    >


    IIUC you can specialize *any* template in namespace std aslong as
    the specialization is dependant an a UDT not defined in namespace std.
    I.e. this should be Ok:

    class myclass {};
    namespace std
    {
    template < typename Alloc >
    class vector< myclass, Alloc >
    {
    };
    }

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Sep 21, 2003
    #8
  9. Steffen Brinkmann

    WW Guest

    Rob Williscroft wrote:
    [SNIP]
    > IIUC you can specialize *any* template in namespace std aslong as
    > the specialization is dependant an a UDT not defined in namespace std.
    > I.e. this should be Ok:
    >
    > class myclass {};
    > namespace std
    > {
    > template < typename Alloc >
    > class vector< myclass, Alloc >
    > {
    > };
    > }


    Chapter and verse?

    --
    WW aka Attila
     
    WW, Sep 21, 2003
    #9
  10. WW wrote in news:bkk5ha$5ne$:

    > Rob Williscroft wrote:
    > [SNIP]
    >> IIUC you can specialize *any* template in namespace std aslong as
    >> the specialization is dependant an a UDT not defined in namespace std.
    >> I.e. this should be Ok:
    >>
    >> class myclass {};
    >> namespace std
    >> {
    >> template < typename Alloc >
    >> class vector< myclass, Alloc >
    >> {
    >> };
    >> }

    >
    > Chapter and verse?
    >


    17.4.3.1/1 - but I missed "... results in undefined ... and unless the
    specialization meets the standard library requirements for the original
    ...." so my example wouldn't be Ok (that "IIUC" wasn't wasted after all).

    Rob.
    --
    http://www.victim-prime.dsl.pipex.com/
     
    Rob Williscroft, Sep 21, 2003
    #10
  11. Steffen Brinkmann

    WW Guest

    Rob Williscroft wrote:
    [SNIP]
    > 17.4.3.1/1 - but I missed "... results in undefined ... and unless the
    > specialization meets the standard library requirements for the
    > original ..." so my example wouldn't be Ok (that "IIUC" wasn't wasted
    > after all).


    That's what I thought. :)

    --
    WW aka Attila
     
    WW, Sep 21, 2003
    #11
  12. Jerry Coffin <> wrote in message news:<>...
    >
    > See $ 17.4.3.1/1 for the exact requirements.


    Where do I find that?

    Steffen
     
    Steffen Brinkmann, Sep 21, 2003
    #12
  13. "WW" <> wrote in message news:<bkipak$5q2$>...

    >
    > > template<typename _Tp, template<typename> class _Container,

    >
    > Standard container templates take many more arguments.


    I copied that method from Bruce Eckel's "Thinking in C++". An dit
    works... Do you mean an explicit Reference?

    > > _Container<_Tp>::iterator __iter=__cont.begin();

    > typename _Container<_Tp>::iterator __iter=__cont.begin();


    THANKSALOT! That was it! typical case of blindness after hours of
    nightly programming!

    >
    > Seems that 3.3 has two phase name lookup implemented or at least it refuses
    > to guess what is a type anymore.


    Why?

    Thanks again, Steffen
     
    Steffen Brinkmann, Sep 21, 2003
    #13
  14. Howard Hinnant <> wrote in message news:<>...

    > You might try out std::bind2nd in <functional> and stick with the
    > std::transform:
    >
    > std::transform(v.begin(), v.end(), v.begin(),
    > std::bind2nd(std::multiplies<int>(), 3));
    >


    That was my first attempt and it worked fine. but I like
    short-to-write function calls like:

    std::transform(v,value,std::multiplies<int>());

    > If you get into more complicated operations, boost::bind might provide
    > an answer ( www.boost.org ). boost::bind may eventually be
    > standardized. It has been voted into the first library technical report
    > which indicates an official interest in this library. The above
    > transform translates into bind with:
    >
    > std::transform(v.begin(), v.end(), v.begin(),
    > boost::bind(std::multiplies<int>(), _1, 3));
    >


    Aha, interesting site, thank you!

    Steffen
     
    Steffen Brinkmann, Sep 21, 2003
    #14
  15. Steffen Brinkmann

    Fraser Ross Guest

    > You might try out std::bind2nd in <functional> and stick with the
    > std::transform:
    >
    > std::transform(v.begin(), v.end(), v.begin(),
    > std::bind2nd(std::multiplies<int>(), 3));


    Is there a reason for not using for_each? I have been wondering if for_each
    is faster.

    Fraser.


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/2003
     
    Fraser Ross, Sep 21, 2003
    #15
  16. Steffen Brinkmann

    Jerry Coffin Guest

    In article <>,
    says...
    > Jerry Coffin <> wrote in message news:<>...
    > >
    > > See $ 17.4.3.1/1 for the exact requirements.

    >
    > Where do I find that?


    That's section 17.4.3.1, paragraph 1, of the C++ standard. If you don't
    have a copy, you can get one as a PDF file for $18US from
    webstore.ansi.org (this requires a credit or debit card they'll accept).

    The DIN only seems to sell the paper version, at a much less attractive
    price (242,70 Euros). You can also get it direct from the ISO (in
    either PDF or paper format) for 364 CHF, which is roughly typical for a
    paper copy, but clearly pretty high for a PDF.

    Various other national bodies sell it as well, but I don't know of any
    that sell the PDF for less than ANSI does. If you decided to get the
    paper version, shipping costs would probably favor buying locally.

    --
    Later,
    Jerry.

    The universe is a figment of its own imagination.
     
    Jerry Coffin, Sep 21, 2003
    #16
  17. In article <>,
    "Fraser Ross" <fraserATmembers.v21.co.unitedkingdom> wrote:

    > > You might try out std::bind2nd in <functional> and stick with the
    > > std::transform:
    > >
    > > std::transform(v.begin(), v.end(), v.begin(),
    > > std::bind2nd(std::multiplies<int>(), 3));

    >
    > Is there a reason for not using for_each? I have been wondering if for_each
    > is faster.


    I think it would be a matter of convenience rather than performance.
    for_each could certainly be used here. But the use of for_each looks
    more complicated to me than does transform.

    On the inside transform will do something like:

    *target = op(*source);

    whereas for_each will do something like:

    op(*source);

    In our example, we're wanting to do:

    *target = *target * 3;

    or maybe:

    *target *= 3;

    So to use for_each you have to cook up a functor that takes a single
    argument and performs the desired operation (both times and assign, or
    the combined *=). There is no std::multiply_assign, but you could roll
    your own:

    template <class T, class U = T>
    struct multiply_assign
    : public std::binary_function<T, U, T>
    {
    T& operator()(T& t, const U& u) const
    {return t *= u;}
    };

    Then you could use this with for_each and bind2nd like:

    std::for_each(v.begin(), v.end(),
    std::bind2nd(multiply_assign<int>(), 3));

    I would expect this example to have the same performance as the
    transform version, but I have not tested that expectation.

    -Howard
     
    Howard Hinnant, Sep 21, 2003
    #17
  18. Steffen Brinkmann

    Fraser Ross Guest

    transform might be as fast if return value optimisation is used. I would
    prefer another algorithm for when the output iterator is the same as an
    input iterator and two ranges are used. That isn't the situation the OP
    has. e.g.

    template <class ForwardIterator, class InputIterator, class BinaryOperation>
    BinaryOperation transform_each (ForwardIterator first1, InputIterator last1,
    InputIterator first2, BinaryOperation binary_op) {
    while (first1 != last1) {
    binary_op(*first1, *first2);
    ++first1, ++first2;
    }
    return binary_op;
    }


    Fraser.


    ---
    Outgoing mail is certified Virus Free.
    Checked by AVG anti-virus system (http://www.grisoft.com).
    Version: 6.0.520 / Virus Database: 318 - Release Date: 18/09/2003
     
    Fraser Ross, Sep 21, 2003
    #18
  19. Sorry, pressed the wrong key.
    >
    > Is there a reason for not using for_each?


    As I said, I'd have to write sth like

    void f(double val){ val+=2.;}

    and then call

    for_each(v.begin(),v.end(),f());

    What, if it is noct 2. but a value that is calculated at runtime?

    Steffen
     
    Steffen Brinkmann, Sep 22, 2003
    #19
  20. Steffen Brinkmann

    red floyd Guest

    Steffen Brinkmann wrote:

    > Sorry, pressed the wrong key.
    >
    >>Is there a reason for not using for_each?

    >
    >
    > As I said, I'd have to write sth like
    >
    > void f(double val){ val+=2.;}
    >
    > and then call
    >
    > for_each(v.begin(),v.end(),f());
    >
    > What, if it is noct 2. but a value that is calculated at runtime?
    >
    > Steffen


    use a functor... There's probably a standard one that I can't remember at this time, but here's one,
    and I realize I may have the syntax/semantics of for_each() slightly messed up. Please have pity, gurus!

    struct add_value {
    double val;
    add_value (val_) : val(val_) { }
    double operator()(double& d) { return d += val; }
    }

    then you can call:

    for_each(v.begin(), v.end(), add_value(some_calculated_value_here));
     
    red floyd, Sep 22, 2003
    #20
    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. Patrick Guio

    STL transform algorithm

    Patrick Guio, Nov 2, 2005, in forum: C++
    Replies:
    9
    Views:
    561
    Clark S. Cox III
    Nov 2, 2005
  2. Piotr

    STL transform algorithm

    Piotr, Feb 14, 2006, in forum: C++
    Replies:
    5
    Views:
    447
    Daniel T.
    Feb 14, 2006
  3. Diego Martins

    Transform algorithm 1-n, n-1 or m-n

    Diego Martins, Oct 18, 2006, in forum: C++
    Replies:
    2
    Views:
    306
    Fraser Ross
    Oct 19, 2006
  4. suresh
    Replies:
    2
    Views:
    333
    suresh
    Dec 30, 2007
  5. Gerald I. Evenden

    Problem with <algorithm> transform

    Gerald I. Evenden, Jan 6, 2008, in forum: C++
    Replies:
    13
    Views:
    559
    James Kanze
    Jan 8, 2008
Loading...

Share This Page