LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   how to sort std::vector<double>? (https://www.linuxquestions.org/questions/programming-9/how-to-sort-std-vector-double-371925/)

markhod 10-11-2005 11:07 AM

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

Hivemind 10-11-2005 11:40 AM

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


sirclif 10-11-2005 11:42 AM

check here .

jtshaw 10-11-2005 12:58 PM

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;
 }
}


markhod 10-12-2005 04:21 AM

Thanks for all the help. I solved my problem now.

Cheers,

Mark


All times are GMT -5. The time now is 12:54 PM.