set begin() retrieved the minimum element?

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

    #16
    Re: set begin() retrieved the minimum element?


    "Andre Kostur" <nntpspam@kostu r.net> wrote in message
    news:Xns967F45F 077886nntpspamk osturnet@207.35 .177.134...[color=blue]
    > "Chris Theis" <christian.thei s@nospam.cern.c h> wrote in
    > news:d9gvk3$pjb $1@sunnews.cern .ch:
    >[color=green]
    > >
    > > "Rolf Magnus" <ramagnus@t-online.de> wrote in message
    > > news:d9gs9q$vp4 $02$1@news.t-online.com...[color=darkred]
    > >> pmatos wrote:
    > >>
    > >>> Hi,
    > >>>
    > >>> If I have set<unsigned int> s; will *(s.begin()) give me the minimum
    > >>> element of the set?
    > >>
    > >> Yes, though I'd write s.front() instead.
    > >>[/color]
    > >
    > > AFAIK front is only supplied by vectors, deques and lists. However,
    > > assuming that we have such a container, why would you write front()
    > > instead of begin()? This is just out of curiosity because I tend to
    > > use begin() and I guess that you have a good reason for the
    > > suggestion.[/color]
    >
    > front() returns a reference to the first element in the container, begin
    > () returns an iterator to the first element in the container.
    >
    > So effectively:
    >
    > T & container::fron t()
    > {
    > return *begin();
    > }
    >[/color]

    Looks like I'm already so used to iterators that my head is so clouded that
    I didn't think of this ;-)

    Cheers
    Chris


    Comment

    • Steven T. Hatton

      #17
      Re: set begin() retrieved the minimum element?

      Chris Theis wrote:
      [color=blue]
      >
      > "Andre Kostur" <nntpspam@kostu r.net> wrote in message[/color]
      [color=blue][color=green]
      >> front() returns a reference to the first element in the container, begin
      >> () returns an iterator to the first element in the container.
      >>
      >> So effectively:
      >>
      >> T & container::fron t()
      >> {
      >> return *begin();
      >> }
      >>[/color]
      >
      > Looks like I'm already so used to iterators that my head is so clouded
      > that I didn't think of this ;-)
      >
      > Cheers
      > Chris[/color]

      I haven't tested it, but I strongly suspect that will _not_ work, as is. I
      believe it needs to be:

      const T & container::fron t() { return *begin(); }

      The references to elements of node-based containers such as list, set, map,
      multiset, and multimap, are, I believe, a bit more reliable than references
      to elements of array based containers such as vector and deque. As a
      general rule, holding onto references to elements of a container is
      probably a bad practice. I speak from the basis of advise more than
      experience.

      --
      If our hypothesis is about anything and not about some one or more
      particular things, then our deductions constitute mathematics. Thus
      mathematics may be defined as the subject in which we never know what we
      are talking about, nor whether what we are saying is true.-Bertrand Russell

      Comment

      • Andre Kostur

        #18
        Re: set begin() retrieved the minimum element?

        "Steven T. Hatton" <chattengau@ger mania.sup> wrote in
        news:jsCdnTim7b jUNyDfRVn-hg@speakeasy.ne t:
        [color=blue]
        > Chris Theis wrote:
        >[color=green]
        >>
        >> "Andre Kostur" <nntpspam@kostu r.net> wrote in message[/color]
        >[color=green][color=darkred]
        >>> front() returns a reference to the first element in the container,
        >>> begin () returns an iterator to the first element in the container.
        >>>
        >>> So effectively:
        >>>
        >>> T & container::fron t()
        >>> {
        >>> return *begin();
        >>> }
        >>>[/color]
        >>
        >> Looks like I'm already so used to iterators that my head is so
        >> clouded that I didn't think of this ;-)
        >>
        >> Cheers
        >> Chris[/color]
        >
        > I haven't tested it, but I strongly suspect that will _not_ work, as
        > is. I believe it needs to be:
        >
        > const T & container::fron t() { return *begin(); }[/color]

        Depends on the container, of course. list certainly doesn't require the
        const. set may, and map doesn't. (It may be a pair<const K, V>, but the
        pair itself isn't const)
        [color=blue]
        > The references to elements of node-based containers such as list, set,
        > map, multiset, and multimap, are, I believe, a bit more reliable than
        > references to elements of array based containers such as vector and[/color]

        I'd prefer to use the phrase 'stable' (persistent?).
        [color=blue]
        > deque. As a general rule, holding onto references to elements of a
        > container is probably a bad practice. I speak from the basis of
        > advise more than experience.[/color]

        Bad practice? Not too sure about that. However, one would be encouraged
        to research the appropriate iterator and reference lifetimes of the
        various containers that you are using...

        Comment

        • Steven T. Hatton

          #19
          Re: set begin() retrieved the minimum element?

          Andre Kostur wrote:
          [color=blue]
          > "Steven T. Hatton" <chattengau@ger mania.sup> wrote in
          > news:jsCdnTim7b jUNyDfRVn-hg@speakeasy.ne t:[/color]
          [...][color=blue][color=green]
          >> I haven't tested it, but I strongly suspect that will _not_ work, as
          >> is. I believe it needs to be:
          >>
          >> const T & container::fron t() { return *begin(); }[/color]
          >
          > Depends on the container, of course. list certainly doesn't require the
          > const. set may, and map doesn't. (It may be a pair<const K, V>, but the
          > pair itself isn't const)[/color]

          I thought we were talking strictly about sets in the previous discussion.
          [color=blue][color=green]
          >> The references to elements of node-based containers such as list, set,
          >> map, multiset, and multimap, are, I believe, a bit more reliable than
          >> references to elements of array based containers such as vector and[/color]
          >
          > I'd prefer to use the phrase 'stable' (persistent?).[/color]

          Actually, after reading chapter 6 of TC++SL, it seems vectors are the only
          containers that will present a problem when they are resized. Of course
          any reference to an element that is erased, popped, or list::remove()d ,
          will present a problem. Inserts are a different story than pushes, and
          iterators do not behave exactly as do references. Iterators are more
          easily invalidated.
          [color=blue][color=green]
          >> deque. As a general rule, holding onto references to elements of a
          >> container is probably a bad practice. I speak from the basis of
          >> advise more than experience.[/color]
          >
          > Bad practice? Not too sure about that. However, one would be encouraged
          > to research the appropriate iterator and reference lifetimes of the
          > various containers that you are using...[/color]

          I guess it depends on what you know will happen to the container in the
          future. I actually have code that keeps a reference to a vector (IIRC - it
          may be a boots::array<>, which would be better). Since I don't intend to
          change the size of the vector after it's allocated, and the number of
          dimensions in 3-space is unlikely to change in the foreseeable future, I'm
          probably pretty safe.
          --
          If our hypothesis is about anything and not about some one or more
          particular things, then our deductions constitute mathematics. Thus
          mathematics may be defined as the subject in which we never know what we
          are talking about, nor whether what we are saying is true.-Bertrand Russell

          Comment

          Working...