copy of list iterator

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

    copy of list iterator

    Hi,

    This is a "list iterator" problem I expect it will copy the list
    iterator (l_iter) to the caller:
    eg.
    list<HashMap>:: iterator AcctConfParser: :find_Acct_rule (string i)
    {
    list<HashMap>:: iterator l_iter;
    HashMap::iterat or m_iter;
    for (l_iter=macro_l ist.begin(); l_iter!=macro_l ist.end(); l_iter++) {
    for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
    if (m_iter->first == "index") {
    if (m_iter->second == i)
    return l_iter;
    }
    }
    }
    }

    This function just return a reference to the caller, how can I make
    modification so that the function will make a copy of the l_iter to the
    caller?

    Thanks
    Sam
  • Rolf Magnus

    #2
    Re: copy of list iterator

    sam wrote:
    [color=blue]
    > Hi,
    >
    > This is a "list iterator" problem I expect it will copy the list
    > iterator (l_iter) to the caller:
    > eg.
    > list<HashMap>:: iterator AcctConfParser: :find_Acct_rule (string i)
    > {
    > list<HashMap>:: iterator l_iter;
    > HashMap::iterat or m_iter;
    > for (l_iter=macro_l ist.begin(); l_iter!=macro_l ist.end(); l_iter++) {
    > for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
    > if (m_iter->first == "index") {
    > if (m_iter->second == i)
    > return l_iter;
    > }
    > }
    > }
    > }
    >
    > This function just return a reference to the caller,[/color]

    Why would it? I don't see any reference in your code at all.
    [color=blue]
    > how can I make modification so that the function will make a copy of the
    > l_iter to the caller?[/color]

    It already does. What makes you believe it doesn't?

    Comment

    • Howard

      #3
      Re: copy of list iterator


      "sam" <sam++@--.com> wrote in message news:d43385$eoe $1@news.hgc.com .hk...[color=blue]
      > Hi,
      >
      > This is a "list iterator" problem I expect it will copy the list iterator
      > (l_iter) to the caller:
      > eg.
      > list<HashMap>:: iterator AcctConfParser: :find_Acct_rule (string i)
      > {
      > list<HashMap>:: iterator l_iter;
      > HashMap::iterat or m_iter;
      > for (l_iter=macro_l ist.begin(); l_iter!=macro_l ist.end(); l_iter++) {
      > for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
      > if (m_iter->first == "index") {
      > if (m_iter->second == i)
      > return l_iter;
      > }
      > }
      > }
      > }
      >
      > This function just return a reference to the caller, how can I make
      > modification so that the function will make a copy of the l_iter to the
      > caller?
      >[/color]

      It already *does* return a copy. It's return type is an iterator, not a
      reference to one, and the return statement says to return a local variable's
      value, which means to copy that value and return it.

      But... from what I see, if the "find" fails, you're missing a return
      statement at the end there. You need to return something in the case where
      it's not found (or else throw an exception, I suppose).

      -Howard


      Comment

      • sam

        #4
        Re: copy of list iterator

        Rolf Magnus wrote:
        [color=blue]
        > sam wrote:
        >
        >[color=green]
        >>Hi,
        >>
        >>This is a "list iterator" problem I expect it will copy the list
        >>iterator (l_iter) to the caller:
        >>eg.
        >>list<HashMap> ::iterator AcctConfParser: :find_Acct_rule (string i)
        >>{
        >> list<HashMap>:: iterator l_iter;
        >> HashMap::iterat or m_iter;
        >> for (l_iter=macro_l ist.begin(); l_iter!=macro_l ist.end(); l_iter++) {
        >> for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
        >> if (m_iter->first == "index") {
        >> if (m_iter->second == i)
        >> return l_iter;
        >> }
        >> }
        >> }
        >>}
        >>
        >>This function just return a reference to the caller,[/color]
        >
        >
        > Why would it? I don't see any reference in your code at all.
        >
        >[color=green]
        >>how can I make modification so that the function will make a copy of the
        >>l_iter to the caller?[/color]
        >
        >
        > It already does. What makes you believe it doesn't?
        >[/color]
        The l_iter-> is a pointer, iterator is a type of pointer?

        Sam.

        Comment

        • Christian Meier

          #5
          Re: copy of list iterator

          1. Iterators are usually implemented as pointers. But they do not have to.
          2. Do not use postfix increment operator for objects if it is not necessary.
          3. Your function must have a return statement for each case. Even if your
          list is empty.

          -Chris

          "sam" <sam++@--.com> schrieb im Newsbeitrag
          news:d4387g$hom $1@news.hgc.com .hk...[color=blue]
          > Rolf Magnus wrote:
          >[color=green]
          > > sam wrote:
          > >
          > >[color=darkred]
          > >>Hi,
          > >>
          > >>This is a "list iterator" problem I expect it will copy the list
          > >>iterator (l_iter) to the caller:
          > >>eg.
          > >>list<HashMap> ::iterator AcctConfParser: :find_Acct_rule (string i)
          > >>{
          > >> list<HashMap>:: iterator l_iter;
          > >> HashMap::iterat or m_iter;
          > >> for (l_iter=macro_l ist.begin(); l_iter!=macro_l ist.end(); l_iter++)[/color][/color][/color]
          {[color=blue][color=green][color=darkred]
          > >> for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
          > >> if (m_iter->first == "index") {
          > >> if (m_iter->second == i)
          > >> return l_iter;
          > >> }
          > >> }
          > >> }
          > >>}
          > >>
          > >>This function just return a reference to the caller,[/color]
          > >
          > >
          > > Why would it? I don't see any reference in your code at all.
          > >
          > >[color=darkred]
          > >>how can I make modification so that the function will make a copy of the
          > >>l_iter to the caller?[/color]
          > >
          > >
          > > It already does. What makes you believe it doesn't?
          > >[/color]
          > The l_iter-> is a pointer, iterator is a type of pointer?
          >
          > Sam.
          >[/color]


          Comment

          • Rolf Magnus

            #6
            Re: copy of list iterator

            Please don't top-post.

            Christian Meier wrote:
            [color=blue]
            > 1. Iterators are usually implemented as pointers.[/color]

            No, they aren't. The only iterators that can be implemented as pointers are
            those for std::vector, and even for those, it varies. In g++, e.g., they
            aren't.

            Comment

            • Rolf Magnus

              #7
              Re: copy of list iterator

              sam wrote:
              [color=blue]
              > Rolf Magnus wrote:
              >[color=green]
              >> sam wrote:
              >>
              >>[color=darkred]
              >>>Hi,
              >>>
              >>>This is a "list iterator" problem I expect it will copy the list
              >>>iterator (l_iter) to the caller:
              >>>eg.
              >>>list<HashMap >::iterator AcctConfParser: :find_Acct_rule (string i)
              >>>{
              >>> list<HashMap>:: iterator l_iter;
              >>> HashMap::iterat or m_iter;
              >>> for (l_iter=macro_l ist.begin(); l_iter!=macro_l ist.end(); l_iter++) {
              >>> for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
              >>> if (m_iter->first == "index") {
              >>> if (m_iter->second == i)
              >>> return l_iter;
              >>> }
              >>> }
              >>> }
              >>>}
              >>>
              >>>This function just return a reference to the caller,[/color]
              >>
              >>
              >> Why would it? I don't see any reference in your code at all.
              >>
              >>[color=darkred]
              >>>how can I make modification so that the function will make a copy of the
              >>>l_iter to the caller?[/color]
              >>
              >>
              >> It already does. What makes you believe it doesn't?
              >>[/color]
              > The l_iter-> is a pointer,[/color]

              l_iter is an iterator, not a pointer. And a pointer is not a reference.
              [color=blue]
              > iterator is a type of pointer?[/color]

              No. It just shares some of its behavior with that of a pointer. Anyway, I'm
              not sure what you want now. Do you want to return l_iter? Or do you want to
              return a reference or a pointer to the element of your container that it's
              associated with? Or do you want to make a copy of that element and return
              that?
              Currently, you're returning a copy of l_iter (i.e. the iterator itself).



              Comment

              • sam

                #8
                Re: copy of list iterator

                Rolf Magnus wrote:[color=blue]
                > sam wrote:
                >
                >[color=green]
                >>Rolf Magnus wrote:
                >>
                >>[color=darkred]
                >>>sam wrote:
                >>>
                >>>
                >>>
                >>>>Hi,
                >>>>
                >>>>This is a "list iterator" problem I expect it will copy the list
                >>>>iterator (l_iter) to the caller:
                >>>>eg.
                >>>>list<HashMa p>::iterator AcctConfParser: :find_Acct_rule (string i)
                >>>>{
                >>>> list<HashMap>:: iterator l_iter;
                >>>> HashMap::iterat or m_iter;
                >>>> for (l_iter=macro_l ist.begin(); l_iter!=macro_l ist.end(); l_iter++) {
                >>>> for (m_iter=l_iter->begin(); m_iter!=l_iter->end(); m_iter++) {
                >>>> if (m_iter->first == "index") {
                >>>> if (m_iter->second == i)
                >>>> return l_iter;
                >>>> }
                >>>> }
                >>>> }
                >>>>}
                >>>>
                >>>>This function just return a reference to the caller,
                >>>
                >>>
                >>>Why would it? I don't see any reference in your code at all.
                >>>
                >>>
                >>>
                >>>>how can I make modification so that the function will make a copy of the
                >>>>l_iter to the caller?
                >>>
                >>>
                >>>It already does. What makes you believe it doesn't?
                >>>[/color]
                >>
                >>The l_iter-> is a pointer,[/color]
                >
                >
                > l_iter is an iterator, not a pointer. And a pointer is not a reference.
                >
                >[color=green]
                >>iterator is a type of pointer?[/color]
                >
                >
                > No. It just shares some of its behavior with that of a pointer. Anyway, I'm
                > not sure what you want now. Do you want to return l_iter? Or do you want to
                > return a reference or a pointer to the element of your container that it's
                > associated with? Or do you want to make a copy of that element and return
                > that?[/color]
                At the moment, I have to return *l_iter to HashMap::iterat or, otherwise
                I will get garbage from the returning item. I may be need to re-test it
                again and see if this is really the case or caused by something else.

                Sam.
                [color=blue]
                > Currently, you're returning a copy of l_iter (i.e. the iterator itself).
                >
                >
                >[/color]

                Comment

                Working...