How can i return an iterator from a fucntion?

Discussion in 'C++' started by TOMERDR, May 22, 2006.

  1. TOMERDR

    TOMERDR Guest

    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.
     
    TOMERDR, May 22, 2006
    #1
    1. Advertising

  2. TOMERDR

    mlimber Guest

    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
     
    mlimber, May 22, 2006
    #2
    1. Advertising

  3. 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


    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
    --
    Please remove capital 'A's when replying by e-mail
    I do not respond to top-posted replies, please don't ask
     
    Victor Bazarov, May 22, 2006
    #3
  4. 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


    "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
     
    Jonathan Mcdougall, May 22, 2006
    #4
  5. TOMERDR

    TOMERDR Guest

    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
    }
    }
     
    TOMERDR, May 22, 2006
    #5
  6. TOMERDR

    Luke Meyers Guest

    TOMERDR wrote:
    > 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
     
    Luke Meyers, May 22, 2006
    #6
  7. TOMERDR wrote:
    > 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
     
    Jonathan Mcdougall, May 22, 2006
    #7
    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. ra294

    Now() Fucntion and CurrentCulture

    ra294, Nov 25, 2004, in forum: ASP .Net
    Replies:
    5
    Views:
    629
    Hans Kesting
    Nov 26, 2004
  2. Replies:
    7
    Views:
    355
  3. sophie_newbie
    Replies:
    4
    Views:
    238
    Paul Hankin
    Oct 18, 2007
  4. aki

    system fucntion in C

    aki, Oct 28, 2010, in forum: C Programming
    Replies:
    1
    Views:
    312
    Seebs
    Oct 28, 2010
  5. Steven D'Aprano

    Re: More Help with python .find fucntion

    Steven D'Aprano, Jan 8, 2011, in forum: Python
    Replies:
    0
    Views:
    514
    Steven D'Aprano
    Jan 8, 2011
Loading...

Share This Page