Original Post
Hi all,
For an autocomplete feature I want to be able to type the begining of a string and by pressing tab get a list of possible completions in a game console window.
This is meant for a CLI, but for simplicity I'll use plain words as an example:
Suppose I have the following keys in a std::map:
apple
apricot
avocado
banana
If I do a partial search for "a", I should get "apple","apricot" and "avocado" if I search for "ap", I should get "apple" and "apricot", and so on.
At this stage I only care about keys, not values, eventually the value will be an object or a callback.
Now, std::map keeps elements in order by the value of its keys, and it contains the member functions lower_bound, upper_bound and equal_range that perform a binary search by key, since all keys must be unique, they all return pretty much the same thing, they take no predicate (comparison function pointer), so the only way of using these to find partial keys is by using a custom Compare function when declaring the map. A custom function that returns equal (false for both a < b and b < a) for partial matches would completely screw the inner working of the map.
So, I can use the general lower_bound, upper_bound and equal_range algorithms that take 2 iterators and optionally a predicate, the problem there is that then the predicate needs to take 2 pairs (IE: less(pair<>,pair<>)) as arguments, since I only need and care about the key at this point, it seems pointless to create a pair and fill in an empty value just to get the partial keys.
I have tried using operator<, and various predicates taking different arguments, but I don't even get them to compile, is there a way around this? how do you even define an operator<(const pair<>&,const string&) ?
Thanks for reading and thanks in advance
For an autocomplete feature I want to be able to type the begining of a string and by pressing tab get a list of possible completions in a game console window.
This is meant for a CLI, but for simplicity I'll use plain words as an example:
Suppose I have the following keys in a std::map:
apple
apricot
avocado
banana
If I do a partial search for "a", I should get "apple","apricot" and "avocado" if I search for "ap", I should get "apple" and "apricot", and so on.
At this stage I only care about keys, not values, eventually the value will be an object or a callback.
Now, std::map keeps elements in order by the value of its keys, and it contains the member functions lower_bound, upper_bound and equal_range that perform a binary search by key, since all keys must be unique, they all return pretty much the same thing, they take no predicate (comparison function pointer), so the only way of using these to find partial keys is by using a custom Compare function when declaring the map. A custom function that returns equal (false for both a < b and b < a) for partial matches would completely screw the inner working of the map.
So, I can use the general lower_bound, upper_bound and equal_range algorithms that take 2 iterators and optionally a predicate, the problem there is that then the predicate needs to take 2 pairs (IE: less(pair<>,pair<>)) as arguments, since I only need and care about the key at this point, it seems pointless to create a pair and fill in an empty value just to get the partial keys.
I have tried using operator<, and various predicates taking different arguments, but I don't even get them to compile, is there a way around this? how do you even define an operator<(const pair<>&,const string&) ?
Thanks for reading and thanks in advance