Query/Maintain a container having two independent keys
Posted on 2013-12-17
Currently in my project is a class that maintains a vector<T> myT which may grow to 1000 elements. The class currently inserts at the current end of the vector. The number of insertions and removals are equal on average.
There are now two new goals. One is to be able to purge several of the oldest elements. If keeping the current vector and current insertion scheme, then since the insertions are appended, the purge would simply remove a group of elements starting at element 0. But the other goal is to derive an int value, key1, for each of the elements during the insertion and then be able to get the element associated with the smallest key1 value and then remove that element. For either type of query/remove pair, I do not want to have to traverse the entire container to determine the minimum value.
I would like to see a C++ example in being able to query a container on two different keys - in my specific case (1) sometimes purging several of the oldest elements and (2) sometimes for getting (and removing) the element having the minimum key1. The container does not have to be a vector; it may have to be custom made.
Note: Cannot use C++11 and 3rd party libraries. (I know there is a boost solution to this.) During the locked container search, retrieval, and remove operation, we can assume that there will be no exceptions.