Which string operation is faster? Which is better?

This is a discussion on Which string operation is faster? Which is better? within the C++ Programming forums, part of the General Programming Boards category; If I have a string passed in that has some characters at the beginning of it that each signify a ...

  1. #1
    Registered User
    Join Date
    Nov 2006
    Posts
    184

    Which string operation is faster? Which is better?

    If I have a string passed in that has some characters at the beginning of it that each signify a type of info and I want to put it in a loop that each time strips off one more character from the beginning, does it make more sense to use "substr" or the pointer?

    Code:
    //Example one
    void doSmth(std::string &str, int length)
    {
         for ( int i = 0; i < length; i++ )
         {
              //some code elided here
              ...
              else doSmth( str.substr( i ), length - i );
         }
    }
    
    //Example two
    void doSmth(std::string &str, int length)
    {
         for ( int i = 0; i < length; i++ )
         {
              //some code elided here
              ...
              else doSmth( *(++str), length - i );
         }
    }

  2. #2
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Calling substr() in a loop would be very inefficient. Just loop through the string counting the prefix characters, until you reach the end of the prefix. Then make a single call to substr() to extract the prefix (or the remainder, whatever the case may be).

  3. #3
    Registered User
    Join Date
    Nov 2006
    Posts
    184
    Quote Originally Posted by brewbuck View Post
    Calling substr() in a loop would be very inefficient. Just loop through the string counting the prefix characters, until you reach the end of the prefix. Then make a single call to substr() to extract the prefix (or the remainder, whatever the case may be).
    What about *(str++)?

  4. #4
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,263
    Quote Originally Posted by 6tr6tr View Post
    What about *(str++)?
    Since str++ is illegal, I would have to say that option is bad.

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,344
    Is length the length of the string? If so, you don't need to pass that around, the string class knows its length (call length() or size()).
    Code:
    void doSmth(std::string &str, int prefixlength)
    You could pass the prefix length if you wanted to implement this recursively, or you could do it iteratively and just remember it.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. recursive string operation..
    By transgalactic2 in forum C Programming
    Replies: 72
    Last Post: 01-09-2009, 04:42 PM
  2. String Class
    By BKurosawa in forum C++ Programming
    Replies: 117
    Last Post: 08-09-2007, 02:02 AM
  3. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 07:52 PM
  4. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 03:23 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21