I've been trying to learn C++ on my own. I've done a lot of programming in Basic and use a simple bubble sort to sort string arrays. Why can't I use the same concept in C++. Why can't I assign the value of an element in an array such as sentence to an array like temp?
For large number of strings, the proposed method is
more efficient than if-else-strcmp approach because
it takes advantage of the inherent search properties
of the map to implement a psuedo-hash function.
although STL does not define what search algorithm is
implemented in a map, the most general case is a binary
tree, and most likely is a red-black tree (MS imp does
that, and I can't imagine why SGI imp wouldn't).
This gives each string lookup to be bouned by O(log(n)).
Then you switch on the enum returned, which, given that
it's sequential, should be compiled into a lookup table,
and that should be a O(1) operation.
the if-else-strcmp approach gives no such guarantee. It's
all based on the order of your if-else chain. Your worst
case scenario is O(n). There is a case where you can
hand-optimize your code to put the more commonly used
strings at the beginning of the chain, but that's just
Consequently, the proposed approach is generally faster
at run-time than the if-else-strcmp method. It can even
be improved to use a hash_map. Checking 3 strings is
inconsequential, however, 20 strings is a different
story -- and that will typically be the case for such
applications as parser.
On a personal note:
it's sad that some of the UNIX advocates did not see the
code in this light, I thought we'd be better than that.
I am not trying to blame the author for writing this code. But some readers may have wrong impressions after looking at this code.
I have seen one comment from a reader saying that when handling thousands of strings this method will be faster than strcmp () method.
That is completely untrue. Becuase, the fastest way to compare a string is to use the strcmp () method. There is no other way which is faster than this. Just using a string case statement in a switch doesn't make it faster. After all the compiler has to convert it to machine code string comparison commands. Where the strcmp () will be converted to a very simple set of machine instructions (or may be a single instruction) where as the mapping statements will result in several more machine instructions which is obviously slower.
However, if you want to handle thousands of strings and compare them the best way is to use arrays or much better to use a dictionary. Using a dictionary it will be much much faster when to compare thousands of strings.
If anyone wants to know how to use a string dictionary to compare thousands or millions of strings, I can send some sample code if you send me a mail.
Faced a problem today when I was working on the design of a dozen of classes.
There I wanted to have a new type defined in each class with "enum" and a
map that converts string value given to it, to this new type. Here's what
I've come up with so far, if anyone is interested (of course, this
is not one of my classes, I made this just to show the idea):