using std::sort;

A

alexhong2001

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!
 
J

John Harrison

alexhong2001 said:
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
 
D

David Harmon

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.
 
D

David Fisher

alexhong2001 said:
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
 
R

rossum

All the standard associative containers are already sorted.


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
 
C

Claudio Puviani

David Harmon said:
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. ;-)
 
R

Rob Williscroft

Claudio Puviani wrote in
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,161
Messages
2,570,892
Members
47,427
Latest member
HildredDic

Latest Threads

Top