How to improve program with string compairing?

Please support our C++ advertiser: Intel Parallel Studio Home
Reply

Join Date: Sep 2004
Posts: 7,630
Reputation: Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute Narue has a reputation beyond repute 
Solved Threads: 718
Team Colleague
Narue's Avatar
Narue Narue is offline Offline
Code Goddess

Re: How to improve program with string compairing?

 
0
  #11
Jul 6th, 2005
>Four characters as in s-i-z-e. It's easier to code with shorter names.
Don't use your infernal logic on me!
I'm here to prove you wrong.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: How to improve program with string compairing?

 
0
  #12
Jul 6th, 2005
Would using the STL iterators be useful here? Do they provide a reverse function? (I would think we could call a reverse function once and then simply do an == check..)
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: How to improve program with string compairing?

 
0
  #13
Jul 6th, 2005
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.

  1. bool are_mirrored(const string &name, const string &name2)
  2. {
  3.  
  4. if (name.size() != name2.size()) {
  5. return false;
  6. }
  7.  
  8. string::const_iterator p = name.begin();
  9. string::const_reverse_iterator r = name2.rbegin();
  10.  
  11. string::const_iterator e = name.end();
  12.  
  13. while (p != e) {
  14. if (*p++ != *r++) {
  15. return false;
  16. }
  17. }
  18.  
  19. return true;
  20. }

Because r is a reverse_iterator, it starts at the last character and faces in the opposite direction. It perceives the string backwards.
Reply With Quote Quick reply to this message  
Join Date: Feb 2005
Posts: 466
Reputation: winbatch is on a distinguished road 
Solved Threads: 18
winbatch's Avatar
winbatch winbatch is offline Offline
Posting Pro in Training

Re: How to improve program with string compairing?

 
0
  #14
Jul 6th, 2005
I guess the real question is speed - whether iterators or array [] type access is faster..
Reply With Quote Quick reply to this message  
Join Date: Jun 2005
Posts: 2,044
Reputation: Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice Rashakil Fol is just really nice 
Solved Threads: 139
Team Colleague
Rashakil Fol's Avatar
Rashakil Fol Rashakil Fol is offline Offline
Super Senior Demiposter

Re: How to improve program with string compairing?

 
0
  #15
Jul 7th, 2005
Originally Posted by winbatch
I guess the real question is speed - whether iterators or array [] type access is faster..
Dereferencing an iterator is faster because a[i] uses one addition operation. But comparing two iterators is slower than checking if an integer is zero. If compiled dumbly, the iterator solution should be slightly faster, but it would not be a very measurable difference.

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.
Reply With Quote Quick reply to this message  
Join Date: Apr 2004
Posts: 4,359
Reputation: Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future Dave Sinkula has a brilliant future 
Solved Threads: 239
Team Colleague
Dave Sinkula's Avatar
Dave Sinkula Dave Sinkula is offline Offline
long time no c

Re: How to improve program with string compairing?

 
0
  #16
Jul 7th, 2005
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.
That's where my thinking had stemmed...
  1. for ( i = 0; i < strlen(s); ++i )
You'd think "the compiler should optimize that to do what I mean". Yet in fact it is prohibited from doing just that.

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
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Other Threads in the C++ Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC