LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


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

Rep: Reputation: 32
'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

Code:
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, 05:51 AM   #2
dakensta
Member
 
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.

HTH
 
Old 12-14-2004, 02:27 PM   #3
Dextrose
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.
 
  


Reply



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 04:28 AM
standard library for graphics jpostma Programming 3 07-19-2004 06:13 AM
Sockets and standard I/O library failure_to Programming 0 05-22-2004 11:55 AM
GCC doesn't find the Standard-Library the_styler Linux - Software 11 01-15-2004 08:09 PM
C++ standard library. Thetargos Programming 8 10-09-2003 03:01 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 08:26 AM.

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