Skip to content
Gautier Lefebvre edited this page Feb 13, 2018 · 2 revisions

OrderedList

This is a collection inheriting from a std::list allowing you to keep it in order. This does pretty much the same thing as a std::priority_queue, but allowing you to iterate over the list. If you don't need to iterate over the list, I recommend you to use the std::priority_queue.

You can use it as a std::list, except for the element insertion part (see last part).

Instantiate the list

To keep the elements in order, you need to provide a comparison function to the constructor. The default is:

[] (const C& a, const C& b) -> bool {
    return a < b;
}

Which will keep the objects in ascending order.

If you are ordering pointers to objects, you can do this instead:

fwk::OrderedList<int*> l(
    [] (const int*& a, const int*& b) -> bool {
        return *a < *b; 
    });

Insert an element

In order to keep the list ordered, you must only use this method to insert elements:

OrderedList<int> l;

l.push(3);
l.push(2);
l.push(5);

// when iterating, the list will be {2, 3, 5};