Created
August 3, 2010 22:04
-
-
Save eagsalazar/507239 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#ifndef SORTEDLIST_H | |
#define SORTEDLIST_H | |
#include <QList> | |
#include <QtAlgorithms> | |
template <typename T> | |
class SortedList : public QList<T> | |
{ | |
public: | |
explicit SortedList(); | |
int insert(const T &t); | |
private: | |
QList<T>::iterator itr; | |
}; | |
template <typename T> | |
int SortedList<T>::insert(const T &t) | |
{ | |
// Find where the new item is or should be inserted | |
// This requires that the template class T implement | |
// both operator<() and operator==(). | |
itr = qLowerBound(begin(), end(), t); | |
if(t != at(itr)) | |
{ | |
itr = insert(itr, t); | |
return itr - begin(); | |
} | |
// If we didn't insert anything, return -1. | |
return -1; | |
} | |
#endif // SORTEDLIST_H |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment