C++: concatenate ints in an array?

Posted by Nate on Stack Overflow See other posts from Stack Overflow or by Nate
Published on 2012-03-25T11:03:11Z Indexed on 2012/03/25 11:29 UTC
Read the original article Hit count: 224

As part of a homework assignment I need to concatenate certain values in an array in C++. So, for example if I have:

int v[] = {0,1,2,3,4}

I may need at some point to concatenate v[1] -> v[4] so that I get an int with the value 1234.

I got it working using stringstream, by appending the values onto the stringstream and then converting back to an integer. However, throughout the program there will eventually be about 3 million different permutations of v[] passed to my toInt() function, and the stringstream seems rather expensive (at least when dealing with that many values). it's working, but very slow and I'm trying to do whatever I can to optimize it.

Is there a more optimal way to concatenate ints in an array in C++? I've done some searching and nearly everywhere seems to just suggest using stringstream (which works, but seems to be slowing my program down a lot).

EDIT: Just clarifying, I do need the result to be an int.

© Stack Overflow or respective owner

Related posts about c++

Related posts about arrays