ReDFoX2200 Posted November 10, 2009 Share Posted November 10, 2009 can some one guied me what data structure i should use to help me search faster the problem is I have a list of names and when the user types in a letter, all names containing the given letter should shown eg. name list Jacob Michael Ethan Joshua Daniel Alexander Olivia the user enters O he should gets Jacob Joshua Olivia if he types OB he should get Jacob Link to comment Share on other sites More sharing options...
0 Andre S. Veteran Posted November 10, 2009 Veteran Share Posted November 10, 2009 (edited) The obvious approach (not necessarily fastest) is a vector<string>. When the user types in a letter, iterate the vector, and for each string, if it contains the letter, show it. Have you tried it? If that isn't fast enough (but I wouldn't assume that), you could use a map<char, vector<string&>> where char is one letter of the alphabet, and vector<string&> is a list of references to strings that contain that letter. So you need to compute that map beforehand, but then finding the strings for a given character is immediate. If you need more data (like where is that letter in the string), you can replace the vector<string&> by a vector of a structure containing the string reference plus whatever additional data you need. The general idea is to pre-compute everything that needs to be accessed quickly. Edited November 10, 2009 by Dr_Asik Link to comment Share on other sites More sharing options...
Question
ReDFoX2200
can some one guied me what data structure i should use to help me search faster
the problem is I have a list of names and when the user types in a letter, all names containing the given letter should shown
eg.
name list
Jacob
Michael
Ethan
Joshua
Daniel
Alexander
Olivia
the user enters O
he should gets
Jacob
Joshua
Olivia
if he types OB
he should get
Jacob
Link to comment
Share on other sites
1 answer to this question
Recommended Posts