LinuxQuestions.org
Help answer threads with 0 replies.
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 10-11-2005, 11:07 AM   #1
markhod
Member
 
Registered: Sep 2003
Posts: 103

Rep: Reputation: 15
how to sort std::vector<double>?


Hi,

I have a vector of doubles (std::vector<doubles>). All I want to do is sort this into an order where the first element has the highest value, the second element the second highest value...the last element the lowest value.

The only promising thing I find is std::sort. But I cannot find any documentation on what it does. So I made a test:

std::vector<Double_t> test;
test.push_back(1);
test.push_back(2);

std::cout << "test[0] is " << test[0] << std::endl;
std::cout << "test[1] is " << test[1] << std::endl;

std::cout << "" << std::endl;

std::sort(test.begin(),test.end());
std::cout << "test[0] is " << test[0] << std::endl;
std::cout << "test[1] is " << test[1] << std::endl;

std::cout << "next event " << std::endl;

and this has output:

test[0] is 1
test[1] is 2

test[0] is 1
test[1] is 2
next event

so whatever sort does it doesnt reorder according to highest value. Is there a standard c++ function to do this to a std::vector?

Thanks,

Mark
 
Old 10-11-2005, 11:40 AM   #2
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
Well, std::sort (and std::stable_sort) sorts all elements in the supplied range using operator < (less than). So if you want to sort using > (greater than), you can supply your own comparison function. The following program examplies:
Code:
#include <algorithm>
#include <iostream>
#include <vector>

static bool sort_using_greater_than(double u, double v)
{
   return u > v;
}

int
main()
{
   std::vector<double> v;

   v.push_back(0.3);
   v.push_back(0.1);
   v.push_back(1.2);
   v.push_back(0.01);

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

   for(std::vector<double>::size_type index = 0; index < v.size(); ++index)
      std::cout << v[index] << std::endl;
}
Output:
Code:
1.2
0.3
0.1
0.01
 
Old 10-11-2005, 11:42 AM   #3
sirclif
Member
 
Registered: Sep 2004
Location: south texas
Distribution: fedora core 3,4; gentoo
Posts: 192

Rep: Reputation: 30
check here .
 
Old 10-11-2005, 12:58 PM   #4
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Just to provide another way of doing it (though I agree the custom comparor is the best way)..

Code:
#include <iostream>
#include <vector>
#include <algorithm>

int main (void)
{
 std::vector<int> test;
 test.push_back(2);
 test.push_back(5);
 test.push_back(7);
 test.push_back(3);
 test.push_back(1);
 test.push_back(4);

 for (int i = 0; i < test.size(); i++) {
    std::cout << "test[" << i << "] is " << test[i] << std::endl;
 }

 std::cout << "" << std::endl;
 std::sort(test.begin(),test.end());
 std::cout << "Sorted: " << std::endl;
 for (int i = 0; i < test.size(); i++) {
    std::cout << "test[" << i << "] is " << test[i] << std::endl;
 }

 std::cout << "" << std::endl;
 std::reverse(test.begin(),test.end());
 std::cout << "Reversed: " << std::endl;
 for (int i = 0; i < test.size(); i++) {
    std::cout << "test[" << i << "] is " << test[i] << std::endl;
 }
}
 
Old 10-12-2005, 04:21 AM   #5
markhod
Member
 
Registered: Sep 2003
Posts: 103

Original Poster
Rep: Reputation: 15
Thanks for all the help. I solved my problem now.

Cheers,

Mark
 
  


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
how to access elements of std::vector<int>* markhod Programming 3 08-02-2005 10:17 AM
std::vector <> ::max_size ta0kira Programming 1 04-29-2005 04:59 AM
can i modify int 80 vector to a user-defined vector qqrilxk Linux - Security 1 03-03-2005 08:46 PM
C++, using custom function to sort an std::list R00ts Programming 3 01-03-2005 09:41 AM
Miseries at install from HDD&floppies, with vector ISO& how to boot with vector linux prctom VectorLinux 9 06-29-2004 05:27 AM

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

All times are GMT -5. The time now is 03:26 PM.

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