C++ equivalence of"Apply"

Discussion in 'C++' started by jacob navia, May 16, 2010.

  1. jacob navia

    jacob navia Guest

    Hi

    I am writing a container library in C, and I wonder how some operations
    are done in C++. For instance, for a given container I have a function
    called "Apply" that will call a function passing it each element of the
    container in turn.

    int (*Apply)(List l,int (Applyfn)(void *,void *),void *arg);

    the "arg" argument is passed through to the "Applyfn" function.

    When looking at the C++ documentation of the list container I find no
    straight equivalent even if this operation could be done with an
    iterator or another construct.

    Another "missing" feature is the "Contains" method of C# list class:
    finding whether an element is in the list.

    The "Copy" operation will make a (shallow) copy of a given list. Java
    and C# have a method for this but not C++

    I am doing a "Notes" section to the documentation where I document the
    solutions provided by other languages.

    Thanks in advance for your attention.

    jacob
    jacob navia, May 16, 2010
    #1
    1. Advertising

  2. jacob navia

    Jonathan Lee Guest

    On May 16, 10:43 am, jacob navia <> wrote:
    >         int (*Apply)(List l,int (Applyfn)(void *,void *),void *arg);
    >
    > the "arg" argument is passed through to the "Applyfn" function.


    You could turn Applyfn into a functor and call std::for_each,
    found in <algorithm>

    > Another "missing" feature is the "Contains" method of C# list class:
    > finding whether an element is in the list.


    Use std::find, again in <algorithm>.

    > The "Copy" operation will make a (shallow) copy of a given list. Java
    > and C# have a method for this but not C++


    In C++ these would be lists of different types of things,
    like a list of pointers or a list of references. Or I
    suppose, ideally, a list of smart pointers.

    If you're doing this as part of some garbage collection,
    then you may want to look at using one of the boost
    smart pointers, like intrusive_ptr. Otherwise GC is
    basically up to you.

    On the other hand, if you're trying to do something specific,
    there may be a better "C++ way" of doing it. If you provide
    some details, I'm sure people here can provide you with good
    alternatives.

    --Jonathan
    Jonathan Lee, May 16, 2010
    #2
    1. Advertising

  3. jacob navia

    Jonathan Lee Guest

    On May 16, 11:43 am, Pete Becker <> wrote:
    > On 2010-05-16 11:15:12 -0400, Jonathan Lee said:
    >
    > > On May 16, 10:43 am, jacob navia <> wrote:
    > >>         int (*Apply)(List l,int (Applyfn)(void *,void *),void *arg);

    >
    > >> the "arg" argument is passed through to the "Applyfn" function.

    >
    > > You could turn Applyfn into a functor and call std::for_each,
    > > found in <algorithm>

    >
    > Or just use it as is. Function pointers are valid arguments to for_each.


    I read the "void* arg" part of his Apply function as passing extra
    function arguments. Assuming his function takes arguments, he'd have
    to bind them to make for_each work, no? Hence functor.

    Or maybe I read him wrong.

    --Jonathan
    Jonathan Lee, May 16, 2010
    #3
  4. On 16.05.2010 16:43, * jacob navia:
    > Hi
    >
    > I am writing a container library in C, and I wonder how some operations
    > are done in C++. For instance, for a given container I have a function
    > called "Apply" that will call a function passing it each element of the
    > container in turn.
    >
    > int (*Apply)(List l,int (Applyfn)(void *,void *),void *arg);
    >
    > the "arg" argument is passed through to the "Applyfn" function.


    std::for_each, or just a good old for loop (I prefer the latter).


    > When looking at the C++ documentation of the list container I find no
    > straight equivalent even if this operation could be done with an
    > iterator or another construct.


    Huh.


    > Another "missing" feature is the "Contains" method of C# list class:
    > finding whether an element is in the list.


    std::find...


    > The "Copy" operation will make a (shallow) copy of a given list. Java
    > and C# have a method for this but not C++


    "="


    > I am doing a "Notes" section to the documentation where I document the
    > solutions provided by other languages.
    >
    > Thanks in advance for your attention.



    Cheers & hth.,

    - Alf


    --
    blog at <url: http://alfps.wordpress.com>
    Alf P. Steinbach, May 16, 2010
    #4
  5. jacob navia

    jacob navia Guest

    Thanks for the replies. I did not know about "algorithm" to do that.

    Thanks
    jacob navia, May 16, 2010
    #5
  6. jacob navia

    jacob navia Guest

    Alf P. Steinbach a écrit :
    > On 16.05.2010 16:43, * jacob navia:
    >
    >> The "Copy" operation will make a (shallow) copy of a given list. Java
    >> and C# have a method for this but not C++

    >
    > "="


    You mean that if I assign a list object to another this will work by
    default or that there is an overloaded assignment operator that will
    make a shallow copy?

    Will the objects be copied or only the list headers?
    jacob navia, May 16, 2010
    #6
  7. jacob navia

    Ian Collins Guest

    On 05/17/10 09:56 AM, jacob navia wrote:
    > Alf P. Steinbach a écrit :
    >> On 16.05.2010 16:43, * jacob navia:
    >>
    >>> The "Copy" operation will make a (shallow) copy of a given list. Java
    >>> and C# have a method for this but not C++

    >>
    >> "="

    >
    > You mean that if I assign a list object to another this will work by
    > default or that there is an overloaded assignment operator that will
    > make a shallow copy?


    That depends what you mean by "by default". std::list has an assignment
    operator which is called "by default" when one list is assigned to another.

    > Will the objects be copied or only the list headers?


    Everything. That's what assignment does, to do otherwise would break
    the user defined operators behave like built in operators "rule" of C++.

    --
    Ian Collins
    Ian Collins, May 16, 2010
    #7
  8. jacob navia

    jacob navia Guest

    Ian Collins a écrit :
    > On 05/17/10 09:56 AM, jacob navia wrote:
    >> Alf P. Steinbach a écrit :
    >>> On 16.05.2010 16:43, * jacob navia:
    >>>
    >>>> The "Copy" operation will make a (shallow) copy of a given list. Java
    >>>> and C# have a method for this but not C++
    >>>
    >>> "="

    >>
    >> You mean that if I assign a list object to another this will work by
    >> default or that there is an overloaded assignment operator that will
    >> make a shallow copy?

    >
    > That depends what you mean by "by default". std::list has an assignment
    > operator which is called "by default" when one list is assigned to another.
    >
    >> Will the objects be copied or only the list headers?

    >
    > Everything. That's what assignment does, to do otherwise would break
    > the user defined operators behave like built in operators "rule" of C++.
    >

    OK, then that is exactly what my "Copy" function does.
    Thanks
    jacob navia, May 16, 2010
    #8
  9. Ian Collins <> wrote:
    >> Will the objects be copied or only the list headers?

    >
    > Everything. That's what assignment does, to do otherwise would break
    > the user defined operators behave like built in operators "rule" of C++.


    Well, it only has to look from the outside like everything has been
    duplicated. What it really does internally doesn't really matter as long
    as it behaves like that from the outside.

    Copy-on-write is sometimes used in situations where data is copied around
    a lot but modified rarely. (Standard library container implementations seldom
    use this, though, as it has its problems in other situations.)
    Juha Nieminen, May 18, 2010
    #9
    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. ALuPin

    Re: Equivalence checking

    ALuPin, Apr 5, 2004, in forum: VHDL
    Replies:
    1
    Views:
    855
    Thomas Stanka
    Apr 6, 2004
  2. Forgafun

    Equivalence checkers for clocks

    Forgafun, Oct 1, 2005, in forum: VHDL
    Replies:
    1
    Views:
    454
    Ralf Hildebrandt
    Oct 1, 2005
  3. Rick Jackson

    Equivalence checking

    Rick Jackson, Nov 23, 2005, in forum: VHDL
    Replies:
    5
    Views:
    1,250
  4. MS News \(MS ILM\)
    Replies:
    0
    Views:
    366
    MS News \(MS ILM\)
    Aug 28, 2003
  5. Stefan Siegl
    Replies:
    1
    Views:
    951
    Marrow
    Jul 18, 2003
Loading...

Share This Page