C++ equivalence of"Apply"

J

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.

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
 
J

Jonathan Lee

        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 said:
Another "missing" feature is the "Contains" method of C# list class:
finding whether an element is in the list.

Use std::find said:
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
 
J

Jonathan Lee

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
 
A

Alf P. Steinbach

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
 
J

jacob navia

Alf P. Steinbach a écrit :

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?
 
I

Ian Collins

Alf P. Steinbach a écrit :

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++.
 
J

jacob navia

Ian Collins a écrit :
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.


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
 
J

Juha Nieminen

Ian Collins said:
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.)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,904
Latest member
HealthyVisionsCBDPrice

Latest Threads

Top