473,394 Members | 1,541 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 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 2306

"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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
by: lok | last post by:
i have a class: template <class T1, class T2> class CPairMapping { public: typedef std::pair<T1, T2> ValuePair_t; typedef std::vector<ValuePair_t> ValueList_t; typedef std::binary_function<...
1
by: Ravi | last post by:
I came across code which contains: std::sort<unsigned char*>((newAnswer.begin()+1),newAnswer.end()); with newAnswer defined as std::vector<unsigned char>& newAnswer However upon...
9
by: JasBascom | last post by:
say i had 97456 and 23456 is there already a sort function to check which one begins with the smaller number rearrange it. in this case the bottom number should clearly be rearranged to the...
4
by: hall | last post by:
I accidently overloaded a static member function that I use as predicate in the std::sort() for a vector and ended up with a compiler error. Is this kind of overload not allowed for predicates and...
7
by: Ireneusz SZCZESNIAK | last post by:
I want to sort a vector with the std::sort function. There are two functions: one with two arguments, the other with three arguments. I am using the one with three arguments. I noticed that...
8
by: Manfred | last post by:
Hello I am new to template programming, so i tried the 'example' from http://www.sgi.com/tech/stl/functors.html. I can compile the code but when i want to run the program I get a segmentation...
15
by: Peter Olcott | last post by:
Does anyone know how to do this?
5
by: fade | last post by:
Good afternoon, I need some advice on the following: I've got a class that has a member std::vector<CStringm_vFileName and a member CString m_path; The vector contains a bunch of filenames with...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...

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.