471,613 Members | 2,023 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,613 software developers and data experts.

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 "istringstream" and "ostringstream" 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!
Jul 22 '05 #1
6 2213

"alexhong2001" <al**********@hotmail.com> wrote in message
news:sG*******************@bgtnsc05-news.ops.worldnet.att.net...
Does "std::sort" work only with sequence containers, not associative
containers at all?
All the standard associative containers are already sorted.

Among sequential containers, can it be used with "list", "queue" and other
sequence containers besides "vector"?
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.

Are "istringstream" and "ostringstream" 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
Jul 22 '05 #2
On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong2001"
<al**********@hotmail.com> was alleged to have written:
Does "std::sort" work only with sequence containers, not associative
containers at all?
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.
Among sequential containers, can it be used with "list", "queue" and other
sequence containers besides "vector"?
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).
Are "istringstream" and "ostringstream" 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.


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.

Jul 22 '05 #3
"alexhong2001" <al**********@hotmail.com> asked:
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"?


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
Jul 22 '05 #4
On Mon, 16 Feb 2004 21:42:32 -0000, "John Harrison"
<ja********************@remove.this> wrote:

"alexhong2001" <al**********@hotmail.com> wrote in message
news:sG*******************@bgtnsc05-news.ops.worldnet.att.net...
Does "std::sort" work only with sequence containers, not associative
containers at all?
All the standard associative containers are already sorted.

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


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.

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

Are "istringstream" and "ostringstream" 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


--

The Ultimate Truth is that there is no Ultimate Truth
Jul 22 '05 #5
"David Harmon" <so****@netcom.com> wrote
On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong2001"
<al**********@hotmail.com> was alleged to have written:
Does "std::sort" work only with sequence containers, not associative
containers at all?


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.


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. ;-)
Jul 22 '05 #6
Claudio Puviani wrote in
news:pM**********************@news4.srv.hcvlny.cv. net:
"David Harmon" <so****@netcom.com> wrote
On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong2001"
<al**********@hotmail.com> was alleged to have written:
>Does "std::sort" work only with sequence containers, not associative
>containers at all?


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.


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. ;-)


The confusion arises as acording to the Standard "Associative 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #7

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by lok | last post: by
1 post views Thread by Ravi | last post: by
9 posts views Thread by JasBascom | last post: by
7 posts views Thread by Ireneusz SZCZESNIAK | last post: by
8 posts views Thread by Manfred | last post: by
5 posts views Thread by fade | last post: by
reply views Thread by leo001 | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.