Share your knowledge at the LQ Wiki.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 12-13-2004, 07:24 PM   #1
Registered: Sep 2004
Location: NorCal
Distribution: slackware 10.1 comfy, Solaris10 learning
Posts: 232

Rep: Reputation: 31
'sort' fn. in C++ standard library

I am trying to use 'sort' function from the <algorithm> header. I have a set of real numbers which I put in the vector container, and I want to sort these numbers in the increasing order.

when I use

sort( v[0], v[size]);
and try to compile, I get 3 full screens of crap. Why doesn't this work?
Old 12-14-2004, 06:51 AM   #2
Registered: Jun 2003
Location: SEUK
Distribution: Debian & OS X
Posts: 194

Rep: Reputation: 35
sort, along with the rest of the standard library, uses iterators not values.

An iterator is basically something that can be dereferenced to access the value (a kind of generalised pointer, if you like - but they will rarely actually be a pointer, so don't count on it!)

The standard containers offer functions begin() and end() to address the first and one-after-the-last elements of a container.

To write a loop over you container, you would write:

for (vector<int>::iterator iter = v.begin(); iter != v.end(); ++iter)
*iter = 99; // or something else equally, erm, useful

probably the best way to call this, is to write
sort(v.begin(), v.end());

you might also want to know that you can use the same function with pointers

int array[10] = { /* some values */ };
sort(array, array+10);

If your c++ implementation is up to date, the memory in a vector should be contiguous and you can then write something like this (if you really must):

sort(&v[0], &v[0]+v.size());

Also worth noting that to use this version of sort your real numbers (if they are a class) must have available an operator<

You can provide one if not or make a comparison class that overloads
bool comparison_class:perator() (real_number first, real_number second)
and call it like this:
sort(v.begin(), v.end(), comparison_class())

If all that sounds somewhat excessive, it has benefits later ... and fwiw when yuou compile with optimisations you will very likely not notice any difference in performance, so don't worry about that.

Old 12-14-2004, 03:27 PM   #3
LQ Newbie
Registered: Dec 2004
Location: Orange County, CA
Distribution: Fedora Core 3
Posts: 8

Rep: Reputation: 0
It might be easier for you to start with the C version of qsort first (found in stdlib.h). Unless of course you're required to use the C++ version of sort.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Standard C and C++ Library Documentation elluva Programming 12 10-25-2004 05:28 AM
standard library for graphics jpostma Programming 3 07-19-2004 07:13 AM
Sockets and standard I/O library failure_to Programming 0 05-22-2004 12:55 PM
GCC doesn't find the Standard-Library the_styler Linux - Software 11 01-15-2004 09:09 PM
C++ standard library. Thetargos Programming 8 10-09-2003 04:01 PM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 09:53 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration