Quote:
Originally posted by g_goblin
I have an issue where i need to be able to search an stl container for a given key and then retrieve the first three keys before that value and the first three keys after that value.
Example:
Say I have a set with {1,2,3,4,5,6,7,8,9,10}
I search for the value 5. I then want to retrieve 3 three keys before 5 and three keys after 5.
Is there any way to do that with and stl set or is there a more appropriate container for this?
TIA,
-g-
|
Since order is important for your needs, I think using a container based on sequences would be more natural. A sorted vector is probably your best bet. You can use standard algorithms to sort and search it, and you can access the elements before and after the one you find fairly easily.
If you need to ensure uniquesness of elements (as in the set), just create a set, then use the copy algorithm to copy it to a vector, then sort the vector and you're good to go.
However, if you are going to be inserting more elements into the container at various intervals, this might not be the best way to go. It will vary with circumstance.