return value of function

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Bob

    return value of function

    Hi,

    I have a std::vector, say myVec, of some user defined object, say
    myOb. In my code, I have a function that searches myVec for a
    particular myOb.

    The way I was doing this was searching myVec for the element that has
    a member equal to a value that was passed into the function. For
    example:

    bool myFunc(const std::string& s)
    {
    std::vector<myO b>::iterator i = myVec.begin();

    while(i != myVec.end()) {
    if((*i).name == s) return true;
    }
    return false;
    }

    This hopefully returns true if the element is found, or false if not.
    However, I don't really want to return true or false, I was wanting to
    return either the offset from myVec.begin() (and maybe -1 if not
    found), or a reference to the element itself.

    How should I do this? Basically, I'm not sure how to return an offset
    from begin(), and I didn't know what to return for the reference in
    the case of 'element not found'. Or, should I really be returning the
    iterator, and de-referencing that in my calling program?

    Many thanks for your valued advice,
    Bob.
  • Denis Remezov

    #2
    Re: return value of function

    Bob wrote:[color=blue]
    >
    > Hi,
    >
    > I have a std::vector, say myVec, of some user defined object, say
    > myOb. In my code, I have a function that searches myVec for a
    > particular myOb.
    >
    > The way I was doing this was searching myVec for the element that has
    > a member equal to a value that was passed into the function. For
    > example:
    >
    > bool myFunc(const std::string& s)
    > {
    > std::vector<myO b>::iterator i = myVec.begin();
    >
    > while(i != myVec.end()) {
    > if((*i).name == s) return true;
    > }
    > return false;
    > }
    >
    > This hopefully returns true if the element is found, or false if not.
    > However, I don't really want to return true or false, I was wanting to
    > return either the offset from myVec.begin() (and maybe -1 if not
    > found), or a reference to the element itself.
    >
    > How should I do this? Basically, I'm not sure how to return an offset
    > from begin(), and I didn't know what to return for the reference in
    > the case of 'element not found'. Or, should I really be returning the
    > iterator, and de-referencing that in my calling program?
    >
    > Many thanks for your valued advice,
    > Bob.[/color]

    Without knowing the specifics of your problem, the first idea is to
    return an iterator to the element if found, myVec.end() otherwise.

    Don't write it as above. Look up the standard function template
    std::find_if. All you need to do is to define a predicate function object
    which stores a const std::string& s, accepts myOb and returns true
    if s == myOb.name, something like

    class MyPred {
    const std::string& s_;
    public:
    MyPred(const std::string& s) : s_(s) {}
    bool operator()(cons t myOb& a) const {
    return s_ == a.name;
    }
    };

    Denis

    Comment

    • Jakob Bieling

      #3
      Re: return value of function

      "Bob" <bob_binz@hotma il.com> wrote in message
      news:77c7b0f4.0 405112218.7b0f2 16c@posting.goo gle.com...
      [color=blue]
      > However, I don't really want to return true or false, I was wanting to
      > return either the offset from myVec.begin() (and maybe -1 if not
      > found), or a reference to the element itself.[/color]
      [color=blue]
      > [..] Or, should I really be returning the
      > iterator, and de-referencing that in my calling program?[/color]

      Yes, seems like the best solution. And if no match was found, you return
      myVec.end (). Tho, then you have to check if the returned iterator is valid
      before dereferncing it.

      hth
      --
      jb

      (replace y with x if you want to reply by e-mail)


      Comment

      • John Harrison

        #4
        Re: return value of function


        "Bob" <bob_binz@hotma il.com> wrote in message
        news:77c7b0f4.0 405112218.7b0f2 16c@posting.goo gle.com...[color=blue]
        > Hi,
        >
        > I have a std::vector, say myVec, of some user defined object, say
        > myOb. In my code, I have a function that searches myVec for a
        > particular myOb.
        >
        > The way I was doing this was searching myVec for the element that has
        > a member equal to a value that was passed into the function. For
        > example:
        >
        > bool myFunc(const std::string& s)
        > {
        > std::vector<myO b>::iterator i = myVec.begin();
        >
        > while(i != myVec.end()) {
        > if((*i).name == s) return true;
        > }
        > return false;
        > }
        >
        > This hopefully returns true if the element is found, or false if not.
        > However, I don't really want to return true or false, I was wanting to
        > return either the offset from myVec.begin() (and maybe -1 if not
        > found), or a reference to the element itself.[/color]

        You can't return a reference to the element in the case where the element
        isn't found. You could throw an exception in this case however.

        You could also return a pointer to the element, and a null pointer in the
        not found case.
        [color=blue]
        >
        > How should I do this? Basically, I'm not sure how to return an offset
        > from begin(), and I didn't know what to return for the reference in
        > the case of 'element not found'.[/color]

        return i - myVec.begin();

        Or, should I really be returning the[color=blue]
        > iterator, and de-referencing that in my calling program?[/color]

        Yes probably, although checking the return value against myVec.end() in the
        calling program is tedious.

        john


        Comment

        • Darius.Moos AT esigma-systems DOT de

          #5
          Re: return value of function

          On Wed, 12 May 2004 08:18:00 +0200, Bob wrote:
          [...][color=blue]
          > However, I don't really want to return true or false, I was wanting to
          > return either the offset from myVec.begin() (and maybe -1 if not found),[/color]

          Have a look at distance()

          HTH, Darius.

          Comment

          • Dave Townsend

            #6
            Re: return value of function

            Bob,

            use the stl find() algorithm - this will return the iterator to the
            element you want or end() to indicate t he object was not found,
            why reinvent the wheel when you can rip one off somebody else car :).


            std::vector<myO b>::iterator i find( myVec.begin(), myVec.end(), foo)

            if ( i!= myVec.end())
            {
            // found.
            myObj bar = *i;
            EurekaImFound(b ar);
            }
            else
            {
            // not found.
            }


            Do you really need the offset, the iterator would give you direct
            access to the object anyway.

            dave


            "Bob" <bob_binz@hotma il.com> wrote in message
            news:77c7b0f4.0 405112218.7b0f2 16c@posting.goo gle.com...[color=blue]
            > Hi,
            >
            > I have a std::vector, say myVec, of some user defined object, say
            > myOb. In my code, I have a function that searches myVec for a
            > particular myOb.
            >
            > The way I was doing this was searching myVec for the element that has
            > a member equal to a value that was passed into the function. For
            > example:
            >
            > bool myFunc(const std::string& s)
            > {
            > std::vector<myO b>::iterator i = myVec.begin();
            >
            > while(i != myVec.end()) {
            > if((*i).name == s) return true;
            > }
            > return false;
            > }
            >
            > This hopefully returns true if the element is found, or false if not.
            > However, I don't really want to return true or false, I was wanting to
            > return either the offset from myVec.begin() (and maybe -1 if not
            > found), or a reference to the element itself.
            >
            > How should I do this? Basically, I'm not sure how to return an offset
            > from begin(), and I didn't know what to return for the reference in
            > the case of 'element not found'. Or, should I really be returning the
            > iterator, and de-referencing that in my calling program?
            >
            > Many thanks for your valued advice,
            > Bob.[/color]


            Comment

            Working...