473,387 Members | 1,749 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,387 software developers and data experts.

sorting a Vector

I'm new to STL, so this might be a laugh...

How do a sort the elemens in a vector? There is a sort() function on list,
but not on vector... right? But why?

Suppose I would like a compare-function to compare the elements and not the
operator< for the elements, how i you do that?

Thanks :-)

--
Lasse
Jul 19 '05 #1
3 61935
> I'm new to STL, so this might be a laugh...

How do a sort the elemens in a vector? There is a sort() function on list, but not on vector... right? But why?
The reason that std::list has a sort function is that it doesn't have
random access iterators, so it cannot work with the standalone
std::sort() function.
Suppose I would like a compare-function to compare the elements and not the operator< for the elements, how i you do that?


Take a look at the std::sort() function, which can sort containers with
random access iterators like std::vector:

#include <vector>
#include <algorithm>

int main()
{
std::vector<int> v;
v.push_back(10);
v.push_back(3);
v.push_back(30);
v.push_back(120);
v.push_back(5);
v.push_back(23);

std::sort(v.begin(), v.end());

return 0;
}

--
Peter van Merkerk
peter.van.merkerk(at)dse.nl
Jul 19 '05 #2

"Peter van Merkerk" <me*****@deadspam.com> wrote in message
news:bn************@ID-133164.news.uni-berlin.de...
I'm new to STL, so this might be a laugh...

How do a sort the elemens in a vector? There is a sort() function on

list,
but not on vector... right? But why?


The reason that std::list has a sort function is that it doesn't have
random access iterators, so it cannot work with the standalone
std::sort() function.
Suppose I would like a compare-function to compare the elements and

not the
operator< for the elements, how i you do that?

[SNIP]

You can apply your own sorting criterion by supplying a binary predicate as
the third (optional) parameter to the sort function.

Regards
Chris
Jul 19 '05 #3
Thanks guys, just what I needed :-)

You can apply your own sorting criterion by supplying a binary predicate as the third (optional) parameter to the sort function.

Regards
Chris

Jul 19 '05 #4

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

Similar topics

5
by: Pratyush | last post by:
Hi, Suppose there is a vector of objects of class A, i.e., std::vector<A> vec_A(N); The class A satisifies all the STL vector requirements. Now I wish to add some attributes for each of the...
3
by: cylin | last post by:
Dear all, After sorting a vector, how to get max in this vector? I got a strange result of my test code. ------------------------------------------------- #include <iostream> #include <vector>...
0
by: Alex Vinokur | last post by:
=================================== ------------- Sorting ------------- Comparative performance measurement =================================== Testsuite : Comparing Function Objects to...
18
by: Matthias Kaeppler | last post by:
Hi, in my program, I have to sort containers of objects which can be 2000 items big in some cases. Since STL containers are based around copying and since I need to sort these containers quite...
2
by: fred_stevens | last post by:
Hi all you C boffins: I need to sort a vector of doubles is ascending order. Qsort will return the sorted vector, but I need a vector of the indices of the sorted vector, not the actual sorted...
9
by: Daz | last post by:
Hello people! (This post is best viewed using a monospace font). I need to create a class, which holds 4 elements: std::string ItemName int Calories int Weight int Density
0
by: SvenMathijssen | last post by:
Hi, I've been wrestling with a problem for some time that ought to be fairly simple, but turns out to be very difficult for me to solve. Maybe someone here knows the answer. What I try to do is...
3
by: Pino | last post by:
HI all, I am learning c++ ( just 1 month) and would want an aid for a code. I have a integer vector of length N. ie 3 3 3 4 4 1
11
by: Trent | last post by:
Running this I see that on first run, both bubble and selection sort have 9 sort counts while insertion sort has ZERO. With a sorted list, should this be ZERO for all? Also bsort and Ssort have...
6
by: Tim Frink | last post by:
Hi, I need some help with STL lists. I've a list with pointers to some objects, let's say std::list< ObjectA* myList. The objects of the class ObjectA all have a function "int...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
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
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.