473,799 Members | 2,840 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

sort on vector type

Hi there,

I am reading through accelerated c++, and have just finshed chapter 3.
My question is what sorting algorithm is used on vectors? e.g. if I had:

std::vector<int > scores;
// adding some values to scores
std::sort(score s.begin(), scores.end());

then what algorithm is used? or is this implementation specific?

Thanks
Allan
Jul 19 '05 #1
3 5651
"Allan Bruce" <al*****@TAKEAW AYf2s.com> wrote...
I am reading through accelerated c++, and have just finshed chapter 3.
My question is what sorting algorithm is used on vectors? e.g. if I had:

std::vector<int > scores;
// adding some values to scores
std::sort(score s.begin(), scores.end());

then what algorithm is used? or is this implementation specific?


It is implementation-specific, it is only defined to adhere to
certain performance requirements. It's probably a quick-sort.

Victor
Jul 19 '05 #2

Allan Bruce <al*****@TAKEAW AYf2s.com> wrote in message
news:bg******** **@news.freedom 2surf.net...
Hi there,

I am reading through accelerated c++, and have just finshed chapter 3.
My question is what sorting algorithm is used on vectors? e.g. if I had:

std::vector<int > scores;
// adding some values to scores
std::sort(score s.begin(), scores.end());

then what algorithm is used?
The language standard does not specify the
algorithm, only the 'complexity'.
or is this implementation specific?


Yes.

Also note that this is not specific to 'vector'.
'std::sort' can be used with any container
which supports random access iterators (this
includes arrays).
-Mike

Jul 19 '05 #3
Allan Bruce wrote:
I have programmed my own sorts as macros in c, and just wondered if it was
worthwhile using them over the standard sorts.
Allan


Almost certainly not. The standard routines are much less likely to have
errors, are available everywhere (while your versions may have
non-portable parts), and are probably faster than yours. Besides that,
they are standard. Everyone recognizes them and knows what they mean.
Add to that the fact that macros are bad, and the standard routines fit
with the rest of the standard library framework, and there's really no
good reason to use your own routines, and a number of reasons not to.

-Kevin

--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.

Jul 19 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
11265
by: {AGUT2} {H}-IWIK | last post by:
Guys, I have these headers: #include <stdlib> #include <math> #include <iostream> #include <iomanip> #include <fstream> #include <vector> #include <string> #include <algorithm>
9
13107
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.
6
2894
by: John Black | last post by:
I have a sort statement for vectr like this, sort(vec.begin(), vec.end(), EleSmaller); in running I find that the sort procedure falls into an endless loop, by adding some debug statement I see sort is endlessly doing some comparision on non-exist number! Then I replace sort() with stable_sort(), it works fine. This really confuses me, what might be the reason of such problem? The vector seems
4
3350
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
4
1917
by: Johan | last post by:
Hi, Why does my vector not sort. What I understand is you have to overload the < operator, but that does not work. see code below Thanks Johan
8
16604
by: laniik | last post by:
Hi. I have a problem using STL's built in sort that seems impossible to get around. if i have: -------------------------------- struct object { int val; }
10
4092
by: eiji | last post by:
Hi folks, I have a problem compiling this under VC6! Maybe someone has some minutes to look at this: template<class T> class Compare { public: Compare(){}; virtual ~Compare(){};
8
4196
by: markww | last post by:
Hi, If I have a vector of structs like this: struct IMAGE { unsigned char *pPix; string strName; int nNumber; };
11
3561
by: Magcialking | last post by:
I wanna sort a pointer vector with the algorithm "sort" in STL like this: " int compare(vector<string*>::iterator a,vector<string*>::iterator b){ return (**a)<(**b); } sort(iter,iter_end,compare); "
22
3186
by: sandy | last post by:
I am trying to make a simulated directory structure application for a course. I am using a Vector to store pointers to my Directory objects (as subdirectories of the current object). In my header it looks like this (private section) vector<Directory*Directories;
0
9687
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...
1
10225
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
10027
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
9072
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
7564
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
5463
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...
0
5585
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4139
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
3759
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.