For me the order is very important.
That is why you need to have the elements initialized before you can add them to std::set, or std::multiset.
The set can't order your objects if they don't have a value, so the data member must have a value so that set can sort your objects.
The set doesn't know anything about your data, it just calls the CompareToBase functor, witch must be able to tell if one object is smaller (or not) then the other objects that are in set.
So: you need to have the objects created and initialized (read them form keyboard or from file etc) and then you put them in set/multiset (witch will sort them).
Or, if you are not comfortable yet with set/multiset, use an c-style array of your data type and sort it with std::sort like this:
std::sort(ar, ar+N, CompareToBase); // ar is the array, N is array size, and your CompareToBase functor
std::sort(ar, ar+N, CompareToBase); // ar is the array, N is array size, and your CompareToBase functor
To copy to clipboard, switch view to plain text mode
or an std::vector, like this:
std::sort(vec.begin(), vec.end(), CompareToBase); // vec is std::vector<your_data_type>
std::sort(vec.begin(), vec.end(), CompareToBase); // vec is std::vector<your_data_type>
To copy to clipboard, switch view to plain text mode
You need: #include <algorithm> if you use std::sort, anyway you need your data to be initialized, and vector and array don't sort automatically as you add elements.
Bookmarks