472,353 Members | 1,671 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

Strategy for locating strings based on initial characters

Hello.

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search substring
which should match the beginning of the searched item? The actual goal
is to implement a functionality similar to that found in help indices,
where one can locate an item by gradually typing its initial characters.
I expect that some kind of tree structure be present in the solution,
but I am not sure.

Since I am multiposting this to comp.programming and comp.lang.c++, I
would like to make it on-topic on the latter as well by asking whether
the STL provides any algorithms that could help here, assming, for
instance, that the strings are stored in a std::set or sorted std::vector.

Thank you,

--
Ney André de Mello Zunino
Jul 23 '05 #1
4 1591
Ney André de Mello Zunino wrote:

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search
substring which should match the beginning of the searched item?
The actual goal is to implement a functionality similar to that
found in help indices, where one can locate an item by gradually
typing its initial characters. I expect that some kind of tree
structure be present in the solution, but I am not sure.


Look up "trie".

--
"If you want to post a followup via groups.google.com, don't use
the broken "Reply" link at the bottom of the article. Click on
"show options" at the top of the article, then click on the
"Reply" at the bottom of the article headers." - Keith Thompson

Jul 23 '05 #2
Ney André de Mello Zunino wrote:
Hello.

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search substring
which should match the beginning of the searched item? The actual goal
is to implement a functionality similar to that found in help indices,
where one can locate an item by gradually typing its initial characters.
I expect that some kind of tree structure be present in the solution,
but I am not sure.

Since I am multiposting this to comp.programming and comp.lang.c++, I
would like to make it on-topic on the latter as well by asking whether
the STL provides any algorithms that could help here, assming, for
instance, that the strings are stored in a std::set or sorted std::vector.

Thank you,


I needed something similar at work. My goal was to lookup an object
based on an object name. My implementation used the STL map<key,object>,
because the map stores object sorted and is therefore 2ln(n) for lookups.

The map needs a key that supports the < operator in order to sort the
objects. So I created a class called SortableString that implemented
the < operator using the c function strcmp.

So my template instantiation looked like
map<SortableString, Object*> objectMap;

I believe this sort of implementation would work for what you want to
do. First you would populate the map with a set of SortableStrings with
their text set to the topics of your help articles. The objects would
be pointers to your articles.

When the user types in a partial string you would create a temporary
SortableString object with its text set to the what the user typed in.
You can then use the lower_bound() function to locate the first element
greater than your temporary string (which should be the closest match
amoung all your subject headers. Psuedocode below.

class SortableString {
SortableString(char* string);
~SortableString();
bool operator < (SortableString& other);
};

map<SortableString, ArticleObject*> subjectHeaderMap;
bool AddSubject(char* subject, ArticleObject* article)
{
SortableString* newSubject = new SortableString(subject);
bool rvalue = objectHeaderMap.insert(map<SortableString*,
ArticleObject*>::value_type(newSubject,article).se cond;
return rvalue;
}

bool MatchSubject(char *test_subject, SortableString* found_subject,
ArticleObject** found_article)
{
bool match_found = false;
SortableString* newSubject = new SortableString(subject);
map<SortableString, ArticleObject*>::Iterator iter;

iter = objectHeaderMap.lower_bound(newSubject);
if (iter != objectHeaderMap.end())
{
*found_subject = iter->first;
*found_article = iter->second;
match_found = true;
}

delete newSubject;
return match_found;
}
Jul 23 '05 #3
Ney André de Mello Zunino wrote:
Hello.

Given a sorted collection of strings, what would a good (the best?)
strategy be to allow fast access to an item, based on a search substring
which should match the beginning of the searched item? The actual goal
is to implement a functionality similar to that found in help indices,
where one can locate an item by gradually typing its initial characters.
I expect that some kind of tree structure be present in the solution,
but I am not sure.


Modified binary search should do the job. (Modified, because you're
looking for "first possible match" and "last ditto", and because it
*might* be useful to work incrementally, although I strongly suspect
that would be an unnecessary optimisation.)

--
Chris "electric hedgehog" Dollin
Jul 23 '05 #4

"Ney André de Mello Zunino" <zu****@inf.ufsc.br> wrote in message
news:d4**********@domitilla.aioe.org...
| Hello.
|
| Given a sorted collection of strings, what would a good (the best?)
| strategy be to allow fast access to an item, based on a search substring
| which should match the beginning of the searched item? The actual goal
| is to implement a functionality similar to that found in help indices,
| where one can locate an item by gradually typing its initial characters.
| I expect that some kind of tree structure be present in the solution,
| but I am not sure.
|
| Since I am multiposting this to comp.programming and comp.lang.c++, I
| would like to make it on-topic on the latter as well by asking whether
| the STL provides any algorithms that could help here, assming, for
| instance, that the strings are stored in a std::set or sorted std::vector.

As an idea, I would start by looking into the following:
std::string::find_first_of( Criteria, StartPos );

std::string offers quite a few other members that may also help.

Cheers,
Chris Val
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Steve Jorgensen | last post by:
I'm restarting this thread with a different focus. The project I'm working on now id coming along and will be made to work, and it's too late to...
7
by: Roman Mashak | last post by:
Hello, All! I wonder is it possible to define an array containing strings, not single characters? What I want is array 'table' that will have N...
5
by: kurt sune | last post by:
The code: Dim aLine As String = "cat" & vbNewLine & "dog" & vbNewLine & "fox" & vbNewLine Dim csvColumns1 As String() = aLine.Split(vbNewLine,...
5
by: BBands | last post by:
I'd like to see if a string exists, even approximately, in another. For example if "black" exists in "blakbird" or if "beatles" exists in...
19
by: pkirk25 | last post by:
I wonder if anyone has time to write a small example program based on this data or to critique my own effort? A file called Realm List.html...
25
by: marcin.rzeznicki | last post by:
Hello everyone I've got a little problem with choosing the best decoding strategy for some nasty problem. I have to deal with very large files wich...
4
by: dhinakar_ve | last post by:
Hi All, I am writing a function to generate the strings based on a pattern. For example A will generate A1, A2 and A3. If the pattern is A...
3
by: Tinku | last post by:
#include<stdio.h> main() { char line; scanf("%", line); printf("%s", line); } it will read and print the line but what is "%" in general we...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
by: Naresh1 | last post by:
What is WebLogic Admin Training? WebLogic Admin Training is a specialized program designed to equip individuals with the skills and knowledge...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
2
by: Matthew3360 | last post by:
Hi, I have a python app that i want to be able to get variables from a php page on my webserver. My python app is on my computer. How would I make it...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....
0
by: Rahul1995seven | last post by:
Introduction: In the realm of programming languages, Python has emerged as a powerhouse. With its simplicity, versatility, and robustness, Python...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.