collection find()

  • Thread starter Thread starter howard
  • Start date Start date
H

howard

If you have a sorted collection, how do you find the location ^closest^
to where a particular key would be if an element with key isn't in the
collection?

For example, if you have a SortedArray containing:
{ 1.102, "fox" }
{ 1.592, "cat" }
{ 2.239, "dog" }
how do you get an interator that points to the location where { 1.910,
"otter" } would be inserted? (I guess in this case the functional
equivalent of an iterator is the index of.)

What I'm hoping for is something that looks like:
int index = mySortedArray.find(1.910); // sets index = 2

Thanks,
H

*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
One way to get the index of where an item would be inserted is to
actually insert the item to the collection. Get the index and remove the
item.
Just an idea.
 
That would work (would be dog-ugly, but work) unless the element did
already exist, in which case it would be overwritten.

I'm sure there must be a simple, efficient way to do this. Right now the
only way I can think of to do it is to get the keys and do my own binary
search. Surely there's another way??



*** Sent via Devdex http://www.devdex.com ***
Don't just participate in USENET...get rewarded for it!
 
Back
Top