Sorting Using STL


Click here to change the theme.

I am a novice user of the STL so I hope this is accurate.

This is a sample of sorting using STL. This sample sorts a record of filenames and dates. The list to be sorted can be created using push_back, but the logic to build the records for the list is not shown. To sort a list, the items of the list must have a "operator<" member function. So the declaration of items in the list could be something such as:

class CRecord {
public:
	char m_Filename[_MAX_PATH];
	unsigned int m_Date;
	bool operator==(const CRecord& y) {return (this->m_Date == y.m_Date);}
	bool operator<(const CRecord& y) {return this->m_Date < y.m_Date;}
};

Then if we want to define a record:

vector <CRecord> Records;

Then records can be inserted using:

Records.push_back(Record);

Finally, to sort:

sort(Records.begin(), Records.end());