dictionary - Search all keys with a prefix in map of C++ -
this question has answer here:
- std::map partial match key 4 answers
i'm using map store data. map has big data. want find keys has prefix, limit number of result items 50. want use fastest way search, not iterate through whole map , check, since map structured key-value container already.
for example, if have pairs:
<"foo123", value> <"foo234", value> <"bar12", value> <"foo456", value> <"bar200", value>
if search "foo", suggest me
foo123, foo234, foo456.
there 2 approaches come mind:
the first special container called trie, there no equivalent in c++ standard library, should able locate working versions easily.
the second approach uses fact in
std::map
, key"foo"
sorts before"foo123"
. usingupper_bound
orlower_bound
functions find starting range , there on use linear search.
Comments
Post a Comment