Connecting Tech Pros Worldwide Forums | Help | Site Map

How can i return an iterator from a fucntion?

TOMERDR
Guest
 
Posts: n/a
#1: May 22 '06

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.


mlimber
Guest
 
Posts: n/a
#2: May 22 '06

re: How can i return an iterator from a fucntion?


TOMERDR wrote:[color=blue]
> 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.[/color]

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

Victor Bazarov
Guest
 
Posts: n/a
#3: May 22 '06

re: How can i return an iterator from a fucntion?


TOMERDR wrote:[color=blue]
> 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[/color]

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

Why not?
[color=blue]
> i want to return an iterator to Appointment.[/color]

What's "an iterator to Appointment"?
[color=blue]
> Is it possible?[/color]

Everything is possible, once the requirements are known.
[color=blue]
> Note that i dont want to use exceptions[/color]

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

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask


Jonathan Mcdougall
Guest
 
Posts: n/a
#4: May 22 '06

re: How can i return an iterator from a fucntion?


TOMERDR wrote:[color=blue]
> 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[/color]

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

Why not?
[color=blue]
> i want to return an iterator to Appointment.[/color]

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.
[color=blue]
> Is it possible?[/color]

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();
}
[color=blue]
> Note that i dont want to use exceptions[/color]

Return a pointer instead.


Jonathan

TOMERDR
Guest
 
Posts: n/a
#5: May 22 '06

re: How can i return an iterator from a fucntion?



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
}
}

Luke Meyers
Guest
 
Posts: n/a
#6: May 22 '06

re: How can i return an iterator from a fucntion?


TOMERDR wrote:[color=blue]
> Is it safe to call it from a loop?[/color]

Kind of an odd question. Why wouldn't it be?
[color=blue]
> while(continue){[/color]

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

Throw by value, catch by reference.
[color=blue]
> {
> //do nothing
> }[/color]

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

Jonathan Mcdougall
Guest
 
Posts: n/a
#7: May 22 '06

re: How can i return an iterator from a fucntion?


TOMERDR wrote:[color=blue]
> ok suppose i will use what you suggest:[/color]

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.
[color=blue]
> Appointment& FindAppointment(Duration d)[/color]

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

You might want to typedef the map, it will make such usage much mroe
easy.
[color=blue]
> if (itor != m_Appointments.end())
> return itor->second;
> throw appointment_not_found();
> }
>
> Is it safe to call it from a loop?[/color]

Of course, why not?
[color=blue]
> while(continue){[/color]

'continue' is a keyword.
[color=blue]
> try {
> Appointment app=FindAppointment(GetDuration())[/color]

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

Appointment& app=FindAppointment(GetDuration());
[color=blue]
> AddApointmetToContainer(app)
> }
> catch(appointment_not_found e)[/color]

Always catch by reference:

catch(appointment_not_found& e)
[color=blue]
> {
> //do nothing[/color]

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.
[color=blue]
> }
> }[/color]

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

Closed Thread