-----BEGIN PGP SIGNED MESSAGE----- Hash: SHA1 David Luff schrieb: > 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.
"The C++ Programming language 3rd ed." tells me: Basically you've got 2 options: 1 - create a class with the "<" operator class IACOcode { string the_code; } bool operator<( const IACOcode& a, const IACOcode& b ) { return your ordering; } map<IACOcode, ARP*> apt_map; 2 - create a custom sort order class IACOcode_compare { public: bool operator()( const string& x, const string& y) const { return your ordering; } } map<string, ARP*, IACOcode_compare> apt_map; Then you'll automatically get the desired result with your described lookup. CU, Christian -----BEGIN PGP SIGNATURE----- Version: GnuPG v1.4.0 (MingW32) Comment: Using GnuPG with Thunderbird - http://enigmail.mozdev.org iD8DBQFCBAAwlhWtxOxWNFcRAvoUAKCQmu/HJmzAQ6OZCLwCPJXoNLalPQCfSKB3 TBEVeGwmDCjOwegYbvbj8AQ= =mohP -----END PGP SIGNATURE----- _______________________________________________ Flightgear-devel mailing list Flightgear-devel@flightgear.org http://mail.flightgear.org/mailman/listinfo/flightgear-devel 2f585eeea02e2c79d7b1d8c4963bae2d