Hi folks,

I've run into a tricky problem when using stl map, and am hoping someone might 
be able to point me on the right direction.

I have a map of airports, indexed by string, which is the ICAO code:

map<string, ARP*> apt_map;

Now, I want to emulate the 'search ahead' function of GPS code entry, so that, 
for instance, entering "KC" will cause "KCAD" to be displayed - the first 
airport in the database starting with "KC".  To do this I use the lower_bound 
function, for both "KC" and "KD".  If the returned iterators don't match, then 
there is a valid match for "KC".

map<string, ARP*>::iterator it1, it2;
it1 = apt_map.lower_bound("KC");
it2 = apt_map.lower_bound("KD");

return(it1 == it2 ? NULL : it1->second);

So far, so good.  Now, the problem is that the KLN89 (and probably most/all GPS 
units) regards A->Z as coming before 0->9, whereas the standard string compare 
function regards 0->9 as coming before A->Z.  So in this instance I might get 
"KC52" displayed instead of "KCAD" (there isn't really a KC52, but there are 
many examples outside the US where this bites).  

Now, I can get round this by using a comparison of lower_bound tests for "KC", 
"KCA" and "KD", and it works.  Unfortunately I then have to check for non-alpha 
chars down to the end of the returned string, and re-test any with 'A' 
substituted in place.  It's effective, but really ugly!  I had to think quite 
hard about code I only wrote a week ago to compose the last sentence, and 
that's always a bad sign.  What I'm sure I ought to be able to do is to define 
a custom comparison operator that performs a string comparison where numbers 
are considered to come after letters, and for good measure to do a 
case-insensitive match on the letters.  I want to be able to do something like:

it1 = apt_map.lower_bound("KC", gps_less);

with all the ugly bits tucked away in gps_less which I can get working and them 
forget about.  Unfortunately, I can't find any good example in any of my books 
to do this, nor on the internet, and everything I try is giving me swathes of 
typical gruesome-to-mentally-parse stl error messages.  If anyone has an 
example of how to do this, or any pointers to one, I'd be most grateful.

Cheers - Dave

Flightgear-devel mailing list

Reply via email to