|
// STLSort.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <vector>
#include <algorithm>
#include <iostream>
typedef double DATE;
// Base class we're using to provide a function with a sortable value
class CHistoryItem
{
public:
CHistoryItem(DATE rTimestamp)
{
m_Timestamp = rTimestamp;
}
// Return the time stamp value
DATE& GetTimestamp()
{
return m_Timestamp;
}
// Indicate the item type as a string
virtual char* ItemName() = 0;
// Output the item to a stream
void OutputToStream(std::ostream& os)
{
os << ItemName() << "\t" << m_Timestamp << "\n";
}
protected:
DATE m_Timestamp;
};
// Repair entry
class CRepairItem : public CHistoryItem
{
public:
CRepairItem(DATE rTimestamp)
: CHistoryItem(rTimestamp)
{
}
// Indicate that this is a repair
char* ItemName() { return "Repair: "; }
};
// Installtion entry
class CInstallItem : public CHistoryItem
{
public:
CInstallItem(DATE rTimestamp)
: CHistoryItem(rTimestamp)
{
}
// Indicate that this is a installation
char* ItemName() { return "Install: "; }
};
// Purchase entry
class CPurchaseItem : public CHistoryItem
{
public:
CPurchaseItem(DATE rTimestamp)
: CHistoryItem(rTimestamp)
{
}
// Indicate that this is a installation
char* ItemName() { return "Purchase: "; }
};
///////////////////////////////
// A vector based class to contain our history items
class CHistoryVector : public std::vector<CHistoryItem*>
{
public:
// Make sure that the history items are de-allocated so we don't leak
~CHistoryVector()
{
for (iterator pItem=begin(); pItem != end(); ++pItem)
delete *pItem;
}
};
// Ascending date sorting function
struct SAscendingDateSort
{
bool operator()(CHistoryItem*& rpStart, CHistoryItem*& rpEnd)
{
return rpStart->GetTimestamp() < rpEnd->GetTimestamp();
}
};
// Descending date sorting function
struct SDescendingDateSort
{
bool operator()(CHistoryItem*& rpStart, CHistoryItem*& rpEnd)
{
return rpStart->GetTimestamp() > rpEnd->GetTimestamp();
}
};
// Stuff a vector with data & then sort
int main(int argc, char* argv[])
{
CHistoryVector HistoryVector;
// Put two repair items in the vector
HistoryVector.push_back(new CRepairItem(2*365.0));
HistoryVector.push_back(new CRepairItem(5*365.0));
// Now stick three installations items in the vector
HistoryVector.push_back(new CInstallItem(3*365.0));
HistoryVector.push_back(new CInstallItem (6*365.0));
HistoryVector.push_back(new CInstallItem (4*365.0));
// Finally we need to add a purchase item to the vector
HistoryVector.push_back(new CPurchaseItem (1*365.0));
//==================================
// Sort the items in ascending order
std::sort(HistoryVector.begin(), HistoryVector.end(), SAscendingDateSort());
// Now show the results!
std::cout << "Ascending Sort\n";
for (long lEle=0; lEle < HistoryVector.size(); ++lEle)
HistoryVector[lEle]->OutputToStream(std::cout);
//==================================
// Sort the items in descending order
std::sort(HistoryVector.begin(), HistoryVector.end(), SDescendingDateSort());
// Now show the new results!
std::cout << "\nDescending Sort\n";
for (lEle=0; lEle < HistoryVector.size(); ++lEle)
HistoryVector[lEle]->OutputToStream(std::cout);
getchar(); // Pause for a response
return 0;
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
This member has not yet provided a Biography. Assume it's interesting and varied, and probably something to do with programming.