using std::sort;

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

    using std::sort;

    Does "std::sort" work only with sequence containers, not associative
    containers at all?

    Among sequential containers, can it be used with "list", "queue" and other
    sequence containers besides "vector"?

    Are "istringstr eam" and "ostringstr eam" covered in the book, "STL Tutorial
    and Reference" (second edition) by D. Musser, et al.? Seems like I could
    not find any related topic in the book.

    Thanks for your comments!


  • John Harrison

    #2
    Re: using std::sort;


    "alexhong20 01" <alexhong2001@h otmail.com> wrote in message
    news:sGaYb.3286 8$hR.817769@bgt nsc05-news.ops.worldn et.att.net...[color=blue]
    > Does "std::sort" work only with sequence containers, not associative
    > containers at all?[/color]

    All the standard associative containers are already sorted.
    [color=blue]
    >
    > Among sequential containers, can it be used with "list", "queue" and other
    > sequence containers besides "vector"?[/color]

    It can only be used with containers that have random access iterators. Among
    the standard containers list means vector and deque, it can also be used
    with arrays. List has its own sort method. Queue is not a container but a
    container adapter, it modifies the interface of another container. If you
    need to sort a container then queue isn't for you.
    [color=blue]
    >
    > Are "istringstr eam" and "ostringstr eam" covered in the book, "STL Tutorial
    > and Reference" (second edition) by D. Musser, et al.? Seems like I could
    > not find any related topic in the book.
    >
    > Thanks for your comments!
    >[/color]

    john


    Comment

    • David Harmon

      #3
      Re: using std::sort;

      On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong20 01"
      <alexhong2001@h otmail.com> was alleged to have written:[color=blue]
      >Does "std::sort" work only with sequence containers, not associative
      >containers at all?[/color]

      Associative containers always keep themselves sorted according to the
      predicate they were created with, in order to facilitate finding things.
      They cannot be re-sorted differently.
      [color=blue]
      >Among sequential containers, can it be used with "list", "queue" and other
      >sequence containers besides "vector"?[/color]

      std::sort() requires random access iterators (Stroustrup sect. 18.7.1).
      std::list provides bidirectional iterators so std::sort Not Applicable.
      std::list provides it's own list::sort() method (Stroustrup 17.2.2.1).
      [color=blue]
      >Are "istringstr eam" and "ostringstr eam" covered in the book, "STL Tutorial
      >and Reference" (second edition) by D. Musser, et al.? Seems like I could
      >not find any related topic in the book.[/color]

      Wouldn't surprise me. The STL portion of the standard library usually
      refers to the part derived from the work of Stepanov, which doesn't
      include streams and std::string.

      Comment

      • David Fisher

        #4
        Re: using std::sort;

        "alexhong20 01" <alexhong2001@h otmail.com> asked:
        [color=blue]
        > Does "std::sort" work only with sequence containers, not associative
        > containers at all?
        > Among sequential containers, can it be used with "list", "queue" and other
        > sequence containers besides "vector"?[/color]

        std::sort() requires a pair of "random access iterators" - which applies to
        std::vector, std::deque and array types (int x[20], etc), but not lists. The
        std::list template class has its own custom sorting function, also called
        sort().

        If you want a sorted queue, you might want to use std::deque instead of
        std:queue.

        AFAIK, std::set and std::map elements are stored in sorted order.

        David F


        Comment

        • rossum

          #5
          Re: using std::sort;

          On Mon, 16 Feb 2004 21:42:32 -0000, "John Harrison"
          <jahhajATntlwor ldDOTcom@remove .this> wrote:
          [color=blue]
          >
          >"alexhong200 1" <alexhong2001@h otmail.com> wrote in message
          >news:sGaYb.328 68$hR.817769@bg tnsc05-news.ops.worldn et.att.net...[color=green]
          >> Does "std::sort" work only with sequence containers, not associative
          >> containers at all?[/color]
          >
          >All the standard associative containers are already sorted.
          >[color=green]
          >>
          >> Among sequential containers, can it be used with "list", "queue" and other
          >> sequence containers besides "vector"?[/color]
          >
          >It can only be used with containers that have random access iterators. Among
          >the standard containers list means vector and deque, it can also be used
          >with arrays. List has its own sort method. Queue is not a container but a
          >container adapter, it modifies the interface of another container. If you
          >need to sort a container then queue isn't for you.[/color]
          Not a queue, but perhaps a priority_queue might fit the requirement.
          A priority_queue is automatically sorted by priority so it does not
          have an explicit sort operation.

          rossum
          [color=blue]
          >[color=green]
          >>
          >> Are "istringstr eam" and "ostringstr eam" covered in the book, "STL Tutorial
          >> and Reference" (second edition) by D. Musser, et al.? Seems like I could
          >> not find any related topic in the book.
          >>
          >> Thanks for your comments!
          >>[/color]
          >
          >john
          >[/color]

          --

          The Ultimate Truth is that there is no Ultimate Truth

          Comment

          • Claudio Puviani

            #6
            Re: using std::sort;

            "David Harmon" <source@netcom. com> wrote[color=blue]
            > On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong20 01"
            > <alexhong2001@h otmail.com> was alleged to have written:[color=green]
            > >Does "std::sort" work only with sequence containers, not associative
            > >containers at all?[/color]
            >
            > Associative containers always keep themselves sorted according to the
            > predicate they were created with, in order to facilitate finding things.
            > They cannot be re-sorted differently.[/color]

            Just to avoid confusion, it's not associative containers in general that are
            sorted by the key, but only some specific types. Hash maps and sets are not
            sorted by key, but they are associative containers.

            Claudio Puviani

            P.S.: I'm not singling you out, David. It just doesn't make sense to reply the
            same thing to everyone. ;-)


            Comment

            • Rob Williscroft

              #7
              Re: using std::sort;

              Claudio Puviani wrote in
              news:pMfYb.2655 6$cE3.25848082@ news4.srv.hcvln y.cv.net:
              [color=blue]
              > "David Harmon" <source@netcom. com> wrote[color=green]
              >> On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong20 01"
              >> <alexhong2001@h otmail.com> was alleged to have written:[color=darkred]
              >> >Does "std::sort" work only with sequence containers, not associative
              >> >containers at all?[/color]
              >>
              >> Associative containers always keep themselves sorted according to the
              >> predicate they were created with, in order to facilitate finding
              >> things. They cannot be re-sorted differently.[/color]
              >
              > Just to avoid confusion, it's not associative containers in general
              > that are sorted by the key, but only some specific types. Hash maps
              > and sets are not sorted by key, but they are associative containers.
              >
              > Claudio Puviani
              >
              > P.S.: I'm not singling you out, David. It just doesn't make sense to
              > reply the same thing to everyone. ;-)
              >[/color]

              The confusion arises as acording to the Standard "Associativ e Containers"
              are sorted:

              <quote>
              23.1.2/9

              The fundamental property of iterators of associative containers is
              that they iterate through the containers in the non-descending order
              of keys where non-descending is defined by the comparison that was
              used to construct them. For any two dereferenceable iterators i and j
              such that distance from i to j is positive, value_comp(*j, *i) == false
              </quote>

              Presumably when we get unordered_set<> etc the Standard will introduce
              a new concept "Unordered Associative Container" or some such.

              Rob.
              --

              Comment

              Working...