Click here to Skip to main content
15,920,576 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Handling Stack Overflow Pin
ShilpiP27-Sep-06 20:03
ShilpiP27-Sep-06 20:03 
QuestionFast keyword Search through CSortedArray?? anyone? Pin
Greg Ellis27-Sep-06 9:50
Greg Ellis27-Sep-06 9:50 
AnswerRe: Fast keyword Search through CSortedArray?? anyone? Pin
David Leyva27-Sep-06 10:06
David Leyva27-Sep-06 10:06 
GeneralRe: Fast keyword Search through CSortedArray?? anyone? Pin
Greg Ellis27-Sep-06 10:15
Greg Ellis27-Sep-06 10:15 
GeneralRe: Fast keyword Search through CSortedArray?? anyone? Pin
Steve S27-Sep-06 21:49
Steve S27-Sep-06 21:49 
AnswerRe: Fast keyword Search through CSortedArray?? anyone? Pin
Zac Howland27-Sep-06 10:20
Zac Howland27-Sep-06 10:20 
AnswerRe: Fast keyword Search through CSortedArray?? anyone? Pin
Waldermort27-Sep-06 15:18
Waldermort27-Sep-06 15:18 
AnswerRe: Fast keyword Search through CSortedArray?? anyone? Pin
Stephen Hewitt27-Sep-06 20:40
Stephen Hewitt27-Sep-06 20:40 
Try something like this. It uses binary searches so it will be many, many times faster then using linear searches. It’s a simple console app which reads in words from a file (each word in the file separated by any white space). It is not limited to only matching a single charcter; you can match all words which start with "act" for example.
------------------------

#include <iostream>
#include <fstream>
#include <vector>
#include <string>
#include <algorithm>
#include <iterator>
#include <functional>

struct PrefixMatchLess : std::binary_function<std::string, std::string, bool>
{
bool operator()(const std::string &s, const std::string &prefix) const
{
const std::string sub = s.substr(0, prefix.length());
return sub<prefix;
}
};

struct PrefixMatchGreater : std::binary_function<std::string, std::string, bool>
{
bool operator()(const std::string &prefix, const std::string &s) const
{
const std::string sub = s.substr(0, prefix.length());
return sub>prefix;
}
};

int main()
{
// For notational convenience.
using namespace std;
typedef vector<string> StringList_t;
typedef istream_iterator<string> StringInIt_t;
typedef ostream_iterator<string> StringOutIt_t;

// Open the word list.
ifstream ifs("C:\\Words.txt");
if (!ifs)
{
cerr << "Failed to open word list!" << endl;
return 1;
}

// List for words.
StringList_t words;

// Read in the word list.
copy(StringInIt_t(ifs), StringInIt_t(), back_inserter(words));

// Sort the list and remove duplicates.
sort(words.begin(), words.end());
words.erase(unique(words.begin(), words.end()), words.end());

// Output the word list (diagnostic).
cout << "Word list:" << endl;
copy(words.begin(), words.end(), StringOutIt_t(cout, "\n"));
cout << endl;

for(;;)
{
// Get the prefix to search for.
string word;
cout << "Enter search prefix (! quit): ";
cin >> word;

// Check if the user wants to quit.
if (word=="!")
{
break; // Quit.
}

cout << endl;

// Calculate range.
StringList_t::iterator b = lower_bound(words.begin(), words.end(), word, PrefixMatchLess());
StringList_t::iterator e = upper_bound(b, words.end(), word, PrefixMatchGreater());

// Output results.
cout << "Matching words:" << endl;
copy(b, e, StringOutIt_t(cout, "\n"));
cout << endl;
}

return 0;
}


Steve
QuestionClearing the edit box in ccombobox Pin
lctrncs27-Sep-06 9:30
lctrncs27-Sep-06 9:30 
QuestionRe: Clearing the edit box in ccombobox Pin
David Crow27-Sep-06 10:48
David Crow27-Sep-06 10:48 
AnswerRe: Clearing the edit box in ccombobox Pin
lctrncs27-Sep-06 11:38
lctrncs27-Sep-06 11:38 
QuestionRe: Clearing the edit box in ccombobox Pin
David Crow28-Sep-06 2:44
David Crow28-Sep-06 2:44 
AnswerRe: Clearing the edit box in ccombobox Pin
lctrncs27-Sep-06 11:42
lctrncs27-Sep-06 11:42 
GeneralRe: Clearing the edit box in ccombobox Pin
David Crow28-Sep-06 2:46
David Crow28-Sep-06 2:46 
GeneralRe: Clearing the edit box in ccombobox Pin
lctrncs28-Sep-06 3:54
lctrncs28-Sep-06 3:54 
GeneralRe: Clearing the edit box in ccombobox Pin
David Crow28-Sep-06 4:07
David Crow28-Sep-06 4:07 
GeneralRe: Clearing the edit box in ccombobox Pin
lctrncs28-Sep-06 7:10
lctrncs28-Sep-06 7:10 
QuestionRe: Clearing the edit box in ccombobox Pin
David Crow28-Sep-06 7:44
David Crow28-Sep-06 7:44 
AnswerRe: Clearing the edit box in ccombobox Pin
lctrncs28-Sep-06 10:15
lctrncs28-Sep-06 10:15 
GeneralRe: Clearing the edit box in ccombobox Pin
David Crow28-Sep-06 10:34
David Crow28-Sep-06 10:34 
GeneralRe: Clearing the edit box in ccombobox Pin
lctrncs28-Sep-06 12:46
lctrncs28-Sep-06 12:46 
QuestionRe: Clearing the edit box in ccombobox Pin
David Crow29-Sep-06 2:40
David Crow29-Sep-06 2:40 
AnswerRe: Clearing the edit box in ccombobox Pin
lctrncs29-Sep-06 5:11
lctrncs29-Sep-06 5:11 
QuestionRe: Clearing the edit box in ccombobox Pin
David Crow29-Sep-06 5:18
David Crow29-Sep-06 5:18 
AnswerRe: Clearing the edit box in ccombobox Pin
lctrncs29-Sep-06 5:44
lctrncs29-Sep-06 5:44 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.