473,598 Members | 3,212 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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!
Jul 22 '05 #1
6 2326

"alexhong20 01" <al**********@h otmail.com> wrote in message
news:sG******** ***********@bgt nsc05-news.ops.worldn et.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 "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
Jul 22 '05 #2
On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong20 01"
<al**********@h otmail.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 "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.


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
"alexhong20 01" <al**********@h otmail.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:

"alexhong200 1" <al**********@h otmail.com> wrote in message
news:sG******* ************@bg tnsc05-news.ops.worldn et.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 "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


--

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++, "alexhong20 01"
<al**********@h otmail.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.hcvln y.cv.net:
"David Harmon" <so****@netcom. com> wrote
On Mon, 16 Feb 2004 21:33:12 GMT in comp.lang.c++, "alexhong20 01"
<al**********@h otmail.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 "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.
--
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
3494
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< ValuePair_t, ValuePair_t, bool> ValuePair_IsLess; void SortAscend(const ValuePair_IsLess& isLess_) {
1
2350
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 attempting to compile the above code I got the following errors (I am using g++ ver 3.3.1 on Cygwin)
9
13092
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 top as it lower in numerical order.
4
3337
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 if so, why not? Shouldn the compiler be able to tell which of he overloaded functions to use? The second A::comp() is the one I accidently added and gives the error message (in Borland C++Builder 6) Unit1.cpp E2285 Could not find a match for
7
3538
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 there is a huge overhead of using this function, which comes from copying the function object, i.e., the object that compares the elements of the vector, which is passed to the function not by reference but by value. Now, at the end of this mail...
8
4217
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 fault when the part of std::sort(...) is reached, but I don't understand why. ------------ Code start ---------------------
15
3325
by: Peter Olcott | last post by:
Does anyone know how to do this?
5
3895
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 no path included (no C:\...) eg: my_file2.jpg, my_file1.bmp, etc... and m_path stores the path, eg: C:\folder1 I want to sort this vector according to different criterion, such as
0
7987
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
7899
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
1
8050
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8264
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
6718
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
5850
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5438
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
3897
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
1504
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.