Thread: fast string concat, any ideas?

  1. #1
    Registered User
    Join Date
    Aug 2005
    Posts
    266

    fast string concat, any ideas?

    Hi, lets say i have a number like 9152 ...and its to be expanded as such : 11111111122222
    (thats 9 ones and 5 twos)

    so .. if i want to expand any given number ... the obvious quick solution is something like this..

    Code:
    string expand(string& input)
    {
    	string ret="";
    	for(int i=0;i<input.length();i+=2)
    	{
    		int expand_amount = input[i] - '0';
    		for(int j=0;j < expand_amount; ++j)
    			ret+=input[i+1];
    	}
    	return ret;
    }
    but in this solution ... we do ret.length() string concats , I want to assume that this is horrible for inputs such as 9999999999.

    Anyone have a solution to making this a bit more .....clever?

    (probably avoiding string concats all together)

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,613
    String has an append() method that can append any number of a certain character.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Add up the 1st, 3rd, ..... (input.length()-1)th values before starting. That sum will be related to the final length of ret.

    Incidentally, what do you expect the function to do if input.length() is an odd value?
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  4. #4
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    Quote Originally Posted by whiteflags View Post
    String has an append() method that can append any number of a certain character.
    ah cool, that seems like the tool that would suit this, i think

    Add up the 1st, 3rd, ..... (input.length()-1)th values before starting. That sum will be related to the final length of ret.

    Incidentally, what do you expect the function to do if input.length() is an odd value?
    nah, odd lengths are invalid..

    and yea thats an idea too ,adding up all the odd index numbers in the string.....I misread your post the first time and it actually led me to another idea. (now probably useless since someone mentioned append())

    just using a loop containing ret+=ret; , doubling what I have each step, that way the number of string concats would only have been less than 2*log( result.length() )
    Last edited by rodrigorules; 10-04-2010 at 12:59 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. char Handling, probably typical newbie stuff
    By Neolyth in forum C Programming
    Replies: 16
    Last Post: 06-21-2009, 04:05 AM
  2. Please check my C++
    By csonx_p in forum C++ Programming
    Replies: 263
    Last Post: 07-24-2008, 09:20 AM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. Linked List Help
    By CJ7Mudrover in forum C Programming
    Replies: 9
    Last Post: 03-10-2004, 10:33 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 02:45 PM