• 0

C++ data structure


Question

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

  • 0

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 by Dr_Asik
Link to comment
Share on other sites

This topic is now closed to further replies.
  • Recently Browsing   0 members

    • No registered users viewing this page.