Click here to Skip to main content
15,890,506 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: prediction algorithm Pin
CPallini25-Aug-12 7:24
mveCPallini25-Aug-12 7:24 
AnswerRe: prediction algorithm Pin
Stefan_Lang29-Aug-12 1:48
Stefan_Lang29-Aug-12 1:48 
QuestionHow to provide scrollbar to a dialog box dynamically in MFC.? Pin
Munish batra23-Aug-12 21:36
Munish batra23-Aug-12 21:36 
AnswerRe: How to provide scrollbar to a dialog box dynamically in MFC.? Pin
Maximilien24-Aug-12 7:21
Maximilien24-Aug-12 7:21 
Questionwhich is the best open source library for 2D drawings. Pin
Member 935377623-Aug-12 20:10
Member 935377623-Aug-12 20:10 
AnswerRe: which is the best open source library for 2D drawings. Pin
Software_Developer23-Aug-12 22:45
Software_Developer23-Aug-12 22:45 
QuestionBetter "pattern" for if-else if for strings ? Pin
Maximilien23-Aug-12 3:57
Maximilien23-Aug-12 3:57 
AnswerRe: Better "pattern" for if-else if for strings ? Pin
pasztorpisti23-Aug-12 4:49
pasztorpisti23-Aug-12 4:49 
Two optimization tricks:
1. You store the possible values (A, B, and C, ...) in a sorted array and you search your string in that array with binary halving. Alternatively you can store A,B,C in one of these: map/hashmap/search tree.
2. If you opt for using == and/or != with if/else then you can speed up these operations for strings by storing the hash value for each string. When you compare two strings you first compare the hash values. If the hash is different then you have your result immediately (false for == and true for !=). If the hash for the strings is different then you have to perform the actual comparison on the strings. I usually write a special string class that contains the hash as well. It has the drawback that the hash must be updated before comparisons if the string has been modified since the last hash calculations. Sometimes purely written old legacy code can be tweaked very well with this trick. Use djb2 to hash your strings, for unicode use utf8: http://www.cse.yorku.ca/~oz/hash.html[^]

EDIT: Just noticed my answer isn't exactly the what you are searching for, but I think there is no fine solution to your problem. This problem is similar to the typical factory patterns that are used to instantiate a specific type by name:
C++
template <typename T>
class StringSwitch
{
public:
	void SetInstance(T* instance)
	{
		m_Instance = instance;
	}
	void RegisterHandler(const std::string& s, void (T::*handler)())
	{
		m_SwitchTable[s] = handler;
	}
	bool PerformOperation(const std::string& s)
	{
		auto it = m_SwitchTable.find(s);
		if (it == m_SwitchTable.end())
			return false;
		(m_Instance->*(it->second))();
		return true;
	}
private:
	std::map<std::string,void(T::*)()> m_SwitchTable;
	T* m_Instance;
};


class BlahBlah
{
public:
	BlahBlah()
	{
		m_SwitchTable1.SetInstance(this);
		m_SwitchTable1.RegisterHandler("A", &BlahBlah::StringA);
		m_SwitchTable1.RegisterHandler("B", &BlahBlah::StringB);
		m_SwitchTable1.RegisterHandler("C", &BlahBlah::StringC);
	}

	void StringA()
	{
		printf("%s\n", __FUNCTION__);
	}
	void StringB()
	{
		printf("%s\n", __FUNCTION__);
	}
	void StringC()
	{
		printf("%s\n", __FUNCTION__);
	}

	void Run()
	{
		m_SwitchTable1.PerformOperation("B");
	}

private:
	StringSwitch<BlahBlah> m_SwitchTable1;
};

int main()
{
	BlahBlah b;
	b.Run();
	return 0;
}

Your refactorized if/else is inside BlahBlah::Run()

modified 23-Aug-12 11:09am.

AnswerRe: Better "pattern" for if-else if for strings ? Pin
Mihai Vrinceanu23-Aug-12 5:41
Mihai Vrinceanu23-Aug-12 5:41 
GeneralRe: Better "pattern" for if-else if for strings ? Pin
_Flaviu23-Aug-12 8:36
_Flaviu23-Aug-12 8:36 
SuggestionRe: Better "pattern" for if-else if for strings ? Pin
David Crow23-Aug-12 5:53
David Crow23-Aug-12 5:53 
AnswerRe: Better "pattern" for if-else if for strings ? Pin
jschell23-Aug-12 8:22
jschell23-Aug-12 8:22 
AnswerRe: Better "pattern" for if-else if for strings ? Pin
_Flaviu23-Aug-12 8:33
_Flaviu23-Aug-12 8:33 
AnswerRe: Better "pattern" for if-else if for strings ? Pin
CPallini23-Aug-12 22:54
mveCPallini23-Aug-12 22:54 
AnswerRe: Better "pattern" for if-else if for strings ? Pin
Chris Losinger24-Aug-12 1:20
professionalChris Losinger24-Aug-12 1:20 
GeneralRe: Better "pattern" for if-else if for strings ? Pin
CPallini24-Aug-12 1:53
mveCPallini24-Aug-12 1:53 
GeneralRe: Better "pattern" for if-else if for strings ? Pin
pasztorpisti24-Aug-12 2:20
pasztorpisti24-Aug-12 2:20 
GeneralRe: Better "pattern" for if-else if for strings ? Pin
Maximilien24-Aug-12 4:04
Maximilien24-Aug-12 4:04 
AnswerRe: Better "pattern" for if-else if for strings ? Pin
bkelly1326-Aug-12 15:55
bkelly1326-Aug-12 15:55 
Questionrun bat file using ShellExecute Pin
manju 323-Aug-12 2:16
manju 323-Aug-12 2:16 
AnswerRe: run bat file using ShellExecute Pin
Chris Meech23-Aug-12 2:20
Chris Meech23-Aug-12 2:20 
AnswerRe: run bat file using ShellExecute Pin
pasztorpisti23-Aug-12 3:07
pasztorpisti23-Aug-12 3:07 
QuestionWrite to Text file using fprintf Pin
manju 323-Aug-12 1:06
manju 323-Aug-12 1:06 
AnswerRe: Write to Text file using fprintf Pin
Richard MacCutchan23-Aug-12 1:56
mveRichard MacCutchan23-Aug-12 1:56 
GeneralRe: Write to Text file using fprintf Pin
CPallini23-Aug-12 22:55
mveCPallini23-Aug-12 22:55 

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.