Hi,
How to use std::sort function.
I have class CStopWeight which holds a overloaded operator < to sort the list in Descending Order.
I hve a vector stopWeight which hold the Objects of CStopWeight. I needed to sort them in Descending Order.
How can I call the operator < in the std::sort function.
I tried
std::sort(stopWeight.begin(),stopWeight.end(),CStopWeight< stopWeight) but doesn't work.
How can this be done.
As I m compiling a code written in VC++ for linux using gcc. Earlier there was a statement which was written but doesn't work. It was
std::sort(stopWeight.begin(), &stopWeight[stopWeight.size()]);
I don't know what this meant.
Some Code Segment below.
Many thanks Lalit
class CStopWeight
{
public:
int m_originalStop; // original stop //~AC_STOP_SEQ
int m_stop; // current T3 stop sequence number //~AC_STOP_SEQ
std::string m_order; // order ID //~AC_STOP_SEQ
int m_numStacks; // number of stacks in the stop(order) //~AC_STOP_SEQ
std::vector<double> m_stackWeight; // weights for each stack in the order. //~AC_STOP_SEQ
// cummulativeStackWeight[0] is 1st stack only, cummulativeStackWeight[1] is 1st and 2nd stack etc //~AC_STOP_SEQ
CStopWeight(int originalStop, int stop, std::string order, int numStacks) {m_originalStop = originalStop; m_stop = stop; m_order = order; m_numStacks = numStacks; m_stackWeight.clear();}; //~AC_STOP_SEQ
~CStopWeight() {m_stackWeight.clear();}; //~AC_STOP_SEQ
// declare the following CStopWeight operators so can use the std::sort function. Note the < operator is set up such that it will sort DESCENDING
friend bool operator==(const CStopWeight& stop1, const CStopWeight& stop2); //~AC_STOP_SEQ
friend bool operator<(const CStopWeight& stop1, const CStopWeight& stop2); //~AC_STOP_SEQ
private: };
======================================
std::vector<CStopWeight> stopWeight; // holds stack weights and other info to allow stops weights to be compared
std::vector<CStopWeight>::iterator stopIterator; // <stopWeight> iterator
// Sort the stops DESC. Heavier stops will be sorted to the front
// of <stopWeight>, ligher stops to the rear.
// Note the "<" operator for CStopWeight was overloaded to sort DESC.
// - Assumes stacks sorted within the stop by weight (note: mixed stacks will appear at the front/end of the stop regardless of weight)
// - Assumes every stack has the same width/length (and ignores turns).
std::sort(stopWeight.begin(), &stopWeight[stopWeight.size()]);
//---------------------------------------------------------- // CStopWeight::operator== // Returns true if the axle weights for stop 1 placed before // stop 2 are the same as if stop 1 had been placed after // stop 2 // //---------------------------------------------------------- bool operator==(const CStopWeight& stop1, const CStopWeight& stop2) { int stop1Size; int stop2Size; int i; double axleWeight1; double axleWeight2;
stop1Size = stop1.m_stackWeight.size(); stop2Size = stop2.m_stackWeight.size();
// Stop 1 before Stop 2 axleWeight1 = 0.0; for (i = 0; i < stop1Size; i++) { axleWeight1 += (stop1Size + stop2Size - i) * stop1.m_stackWeight.at(i); } for (i = 0; i < stop2Size; i++) { axleWeight1 += (stop2Size - i) * stop2.m_stackWeight.at(i); }
// Stop 2 before Stop 1 axleWeight2 = 0.0; for (i = 0; i < stop2Size; i++) { axleWeight2 += (stop1Size + stop2Size - i) * stop2.m_stackWeight.at(i); } for (i = 0; i < stop1Size; i++) { axleWeight2 += (stop1Size - i) * stop1.m_stackWeight.at(i); }
return axleWeight1 == axleWeight2; }
//---------------------------------------------------------- // CStopWeight::operator< // Returns true if the axle weights for stop 1 placed before // stop 2 would be larger (NOT smaller!). Note sign // reversal because we want to sort DESCENDING not ASCENDING. //---------------------------------------------------------- bool operator<(const CStopWeight& stop1, const CStopWeight& stop2) {
int stop1Size; int stop2Size; int i; double axleWeight1; double axleWeight2;
stop1Size = stop1.m_stackWeight.size(); stop2Size = stop2.m_stackWeight.size();
// Stop 1 before Stop 2 axleWeight1 = 0.0; for (i = 0; i < stop1Size; i++) { axleWeight1 += (stop1Size + stop2Size - i) * stop1.m_stackWeight.at(i); } for (i = 0; i < stop2Size; i++) { axleWeight1 += (stop2Size - i) * stop2.m_stackWeight.at(i); }
// Stop 2 before Stop 1 axleWeight2 = 0.0; for (i = 0; i < stop2Size; i++) { axleWeight2 += (stop1Size + stop2Size - i) * stop2.m_stackWeight.at(i); } for (i = 0; i < stop1Size; i++) { axleWeight2 += (stop1Size - i) * stop1.m_stackWeight.at(i); }
return axleWeight1 > axleWeight2; }
_________________________________________________________________
Sell what you don?t Need. We help you Ship it out. http://go.msnserver.com/IN/54179.asp Click Here!