fast string concat, any ideas?

This is a discussion on fast string concat, any ideas? within the C++ Programming forums, part of the General Programming Boards category; Hi, lets say i have a number like 9152 ...and its to be expanded as such : 11111111122222 (thats 9 ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,738
    String has an append() method that can append any number of a certain character.

  3. #3
    Registered User
    Join Date
    Jun 2005
    Posts
    6,429
    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%.

  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, 09:33 PM
  5. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM

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