TOMERDR wrote:
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