How can i return an iterator from a fucntion?

T

TOMERDR

Hi,i am new to stl and i have a question regarding iterators

I have class day contains a map of appointments:

map<time_t,Appointment> m_Appointments;

I would like to write a function FindAppointment(Duration duration)
which return stl style result
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator
i want to return an iterator to Appointment.

Is it possible?

Note that i dont want to use exceptions

Thanks in advance.
 
M

mlimber

TOMERDR said:
Hi,i am new to stl and i have a question regarding iterators

I have class day contains a map of appointments:

map<time_t,Appointment> m_Appointments;

I would like to write a function FindAppointment(Duration duration)
which return stl style result
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator
i want to return an iterator to Appointment.

Is it possible?

Note that i dont want to use exceptions

Thanks in advance.

Iterators allow you to traverse *containers*. Is Appointment a
container? If not, do you just want to return the value that your
look-up function (note the spelling) finds? Presumably your function
has some way of translating a Duration (the parameter type you pass in)
to a time_t (the key type for your map) and then calls std::map::find()
with the key to retrieve the value associated with it.

If that's a correct guess at your algorithm, then the iterator your
received from std::map::find() (assuming the find was successful)
refers to a std::pair<time_t,Appointment>. You can then return the
iterator itself, a copy of the Appointment that the iterator refers to,
or a pointer to the Appointment that the iterator refers to. It just
depends on what interface you need for the calling functions.

Re exceptions: std::map::find() doesn't throw an exception by itself,
but adding things to a map, for instance, can do so whether you like it
or not.

Cheers! --M
 
V

Victor Bazarov

TOMERDR said:
Hi,i am new to stl and i have a question regarding iterators

I have class day contains a map of appointments:

map<time_t,Appointment> m_Appointments;

I would like to write a function FindAppointment(Duration duration)
which return stl style result

What's "stl style result"?
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator

Why not?
i want to return an iterator to Appointment.

What's "an iterator to Appointment"?
Is it possible?

Everything is possible, once the requirements are known.
Note that i dont want to use exceptions

How is *that* relevant? I strongly doubt anybody would suggest throwing
an exception just to return a value (although it's actually possible).

V
 
J

Jonathan Mcdougall

TOMERDR said:
Hi,i am new to stl and i have a question regarding iterators

I have class day contains a map of appointments:

map<time_t,Appointment> m_Appointments;

I would like to write a function FindAppointment(Duration duration)
which return stl style result

"stl style result" means nothing.
but i dont want to return an iterator of type
map<time_t,Appointment>::Iterator

Why not?
i want to return an iterator to Appointment.

An iterator is a value that represents an element in a container. An
Appointment is not an element in a container, it is the value in a
key-value pair element.
Is it possible?

You could return a reference to it instead:

Appointment& FindAppointment(Duration d)
{
map<time_t,Appointment>:: iterator itor = ...;
if (itor != m_Appointments.end())
return itor->second;

throw appointment_not_found();
}
Note that i dont want to use exceptions

Return a pointer instead.


Jonathan
 
T

TOMERDR

ok suppose i will use what you suggest:

Appointment& FindAppointment(Duration d)
{
map<time_t,Appointment>:: iterator itor = ...;
if (itor != m_Appointments.end())
return itor->second;
throw appointment_not_found();

}

Is it safe to call it from a loop?

while(continue){
try {
Appointment app=FindAppointment(GetDuration())
AddApointmetToContainer(app)
}
catch(appointment_not_found e)
{
//do nothing
}
}
 
L

Luke Meyers

TOMERDR said:
Is it safe to call it from a loop?

Kind of an odd question. Why wouldn't it be?
while(continue){

Syntax error. "continue" is a C++ keyword that cannot be used in this
context. The parentheses here must contain a boolean expression.
try {
Appointment app=FindAppointment(GetDuration())
AddApointmetToContainer(app)
}
catch(appointment_not_found e)

Throw by value, catch by reference.
{
//do nothing
}

Swallowing exceptions is rarely a reflection of sound design. If the
exception is "designed to be swallowed," you're using exceptions for
flow control, which is bad. Otherwise, swallowing an exception is
basically pretending to know how to handle a condition you don't really
know how to handle, and denying anyone who *does* know how to handle it
the opportunity to do so.

Anyway, I wouldn't want to use exceptions here. Have you heard of the
Null Object Pattern? Alternatively, just return a pointer instead, and
return NULL rather than throwing an exception. Or just return the
silly iterator.

Luke
 
J

Jonathan Mcdougall

TOMERDR said:
ok suppose i will use what you suggest:

Please learn to quote correctly on Usenet. See
http://en.wikipedia.org/wiki/Top-post#Inline_replies for more
informations and http://cfaj.freeshell.org/google/ if you are using
Google Groups.
Appointment& FindAppointment(Duration d)

You might want to pass 'd' by const reference if it is not a [typedef
for a] built-in.
{
map<time_t,Appointment>:: iterator itor = ...;

You might want to typedef the map, it will make such usage much mroe
easy.
if (itor != m_Appointments.end())
return itor->second;
throw appointment_not_found();
}

Is it safe to call it from a loop?

Of course, why not?
while(continue){

'continue' is a keyword.
try {
Appointment app=FindAppointment(GetDuration())

FindAppointment returns a reference, don't copy objects when it is not
necessary.

Appointment& app=FindAppointment(GetDuration());
AddApointmetToContainer(app)
}
catch(appointment_not_found e)

Always catch by reference:

catch(appointment_not_found& e)
{
//do nothing

Err.. are you sure? In this case, just return a pointer instead of a
reference and return 0 if the value was not found. Although it may be
normal in your design not to find some appointments, it certainly looks
weird.

Last thing. Please remember that Usenet is a "public place". What you
write is read by many people. Try to respect netiquette and try to post
valid code (at least, end your statements with semicolons!)


Jonathan
 

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

Staff online

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,832
Latest member
GlennSmall

Latest Threads

Top