displaying in descending order.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#include <iostream>
#include <algorithm>
#include <vector>
using namespace std;

bool myfunction (int i,int j) { return (i<j); }

struct myclass {
  bool operator() (int i,int j) { return (i<j);}
} myobject;

int main () {

	int size;
	double x;
	
	vector<double> myVec;
	cout<<"what is the size of array you want to input?";
	cin>>size;
	double array[size];
	for(int i = 0; i <size; i++)
	{
		cout<<"enter your "<<i+1<<" value";
		cin>>x;
		myVec.push_back(x);
	}
  int myints[] = {32,71,12,45,26,80,53,33};
  vector<int> myvector (myVec.begin(), myVec.end());              // 32 71 12 45 26 80 53 33
  vector<int>::iterator it;

  // using default comparison (operator <):
  sort (myvector.begin(), myvector.begin()+((myVec.size())/2));           //(12 32 45 71)26 80 53 33

  // using function as comp
  sort (myvector.begin()+((myVec.size())/2), myvector.end(), myfunction); // 12 32 45 71(26 33 53 80)

  // using object as comp
  sort (myvector.begin(), myvector.end(), myobject);     //(12 26 32 33 45 53 71 80)

  // print out content:
  cout << "the descending order is";

  
  for (it=myvector.begin(); it!=myvector.end(); ++it)
    cout << " " << *it;
  cout<<endl;
  cout<<"the capacity and size is " <<myvector.capacity()<<" and "<<myvector.size();
  cout << endl;

  return 0;
}


Hello guys, i got it to display in acsending, what i fi want to display in descending? In a normal array you jsut mess with the for loop but it seems in here you need to do something with teh sorting, anyone got any idea?
closed account (D80DSL3A)
Write myfunction to reverse the comparison.
Instead of: bool myfunction (int i,int j) { return (i<j); }
try using: bool myfunction (int i,int j) { return (j<i); }
If you want to display it in descending order but don't necessarily care that it's in that order, you could use reverse iterators @Line 44.
http://cplusplus.com/reference/stl/vector/rbegin/
http://cplusplus.com/reference/stl/vector/rend/

-Albatross
Topic archived. No new replies allowed.