5 ms·
Well, the choice was between having 'z'++ go to 'aa' vs. '{'. In most cases when you are comparing strings you want to do so alphabetically, not by their under
by rll 16y ago
Well, the choice was between having 'z'++ go to 'aa' vs. '{'. In most cases when you are comparing strings you want to do so alphabetically, not by their underlying character code. As mentioned in the thread there, Perl made the same decision. If you want to compare by character code, use ord() on it.
Writing a loop like that is also a bit of a weird edge-case. You would typically just call range('a','z') to get an array of a through z.
- chime 16y agoWhy does it even execute z++? Shouldn't it stop at 'z' since that is set as the end condition of the for loop?
- dangrossman 16y ago'z' + 1 is 'aa' which is not less than 'z' lexicographically, so the end condition is NOT met and the loop continues
- brianpan 16y agoThe for loop runs while $i is less than or equal to 'z'.
- jwr 16y agoAlso, one might wonder why it stops at 'yz' instead of continuing on forever. There is no logic in this insanity — you won't know until you try it out.
- zephyrfalcon 16y agoI think it's because the next value, 'za', is neither equal to 'z' nor "less" than it, so the condition fails, and the loop ends.
- bdr 16y agoThere is a third, correct choice: don't allow incrementing of strings. Everything you say about comparing is true, but that's a separate issue.
- mahmud 16y agos/strings/characters/ [Edit: Giving this further thought, I think languages with mutable strings can overload the increment and decrement operators as a way to resize buffers, or move the fill pointer. In C++ this would actually make perfect sense, for implementing high-performance strings. The ++ can be a synonym for realloc, and the base pointer to the string, along with its length and encoding can be stored in an opaque "struct string" handle. Fast, secure pascal-style strings.]
- seabee 16y agoThis sounds awful and unintuitive. Stick with methods, please.
- mahmud 16y agoHuh? C++ doesn't have "methods". What language are you really thinking of?
- chc 16y ago"Method" is OO parlance for a function that is associated with a class or object. C++ has those.
- mahmud 16y agoIndeed, but I said "for implementing high-performance strings." I would love to hear about the OP's ideas for faster variable-length and adjustable string implementation using C++ member or friend functions (the C++ implementation of "methods".) When it comes to performance, the same design I detailed is used by all the "high-per" libraries that I know of. If we're talking API aesthetics, fast implementations can always be wrapped with something more attractive. P.S. Let me make it clear that I am not advocating operator overloading for resizing strings and that the OP actually does have a point. (sorry if I came off as a douche in that regard.) But implementing fast-strings as classes + methods is not really fastest way in C++.