| | |
How to improve program with string compairing?
Please support our C++ advertiser: Intel Parallel Studio Home
![]() |
Here's a version that uses STL iterators. Reverse iterators make the while loop rather elegant. You can also decrement to backup on a regular iterator, but this version uses a reverse iterator instead.
Because r is a reverse_iterator, it starts at the last character and faces in the opposite direction. It perceives the string backwards.
C++ Syntax (Toggle Plain Text)
bool are_mirrored(const string &name, const string &name2) { if (name.size() != name2.size()) { return false; } string::const_iterator p = name.begin(); string::const_reverse_iterator r = name2.rbegin(); string::const_iterator e = name.end(); while (p != e) { if (*p++ != *r++) { return false; } } return true; }
Because r is a reverse_iterator, it starts at the last character and faces in the opposite direction. It perceives the string backwards.
•
•
•
•
Originally Posted by winbatch
I guess the real question is speed - whether iterators or array [] type access is faster..
With compiler optimizations in play, the end result is up for grabs. Trying to solve this problem as efficiently as possible is rarely worth the programmer's time -- there are some more tricks that you could pull, but most comparisons will have unequal lengths or will be decided unequal very early in the loop -- only the equal strings run all the way through.
•
•
•
•
Originally Posted by Rashakil Fol
This inefficiency is not actually the case. [...]
[...] Only the C library's strlen function has to loop through the string, because C-style strings are simply pointers to arrays of characters that have a zero somewhere at the end.
C++ Syntax (Toggle Plain Text)
for ( i = 0; i < strlen(s); ++i )
Still, why do twice what you can do once. (And yes, there are occasionally reasons.)
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
![]() |
Other Threads in the C++ Forum
- Previous Thread: Game Programming
- Next Thread: small program request
| Thread Tools | Search this Thread |
api array arrays based binary c++ c/c++ calculator char char* class classes code coding compile console conversion count database delete deploy desktop developer directshow dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game givemetehcodez google graph gui homeworkhelp iamthwee ifstream input int integer java lib linkedlist linker linux list loop looping loops map math matrix memory multiple news number numbertoword output pointer problem program programming project python random read recursion recursive reference return rpg sorting string strings temperature template templates test text text-file tree unix url variable vector video visual visualstudio win32 windows winsock wordfrequency wxwidgets






