troubleshooting Question

Query/Maintain a container having two independent keys

Avatar of phoffric
phoffric asked on
C++Programming Theory
23 Comments6 Solutions393 ViewsLast Modified:
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.

Join our community to see this answer!
Unlock 6 Answers and 23 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 6 Answers and 23 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros