Any standard way/function like appendtoend(255, 21) = 25521
int x = 255
int y = 22
int z = 300
I'd like to be able to simply append them to variable xyz so that with a function call xyz would become 25522300.
I know of a bad way to make a function like this but I'm wondering if anyone has an efficient way or knows of a standard C++ function that does this?
If you know how many digits they're supposed to be then just multiply by 10^x, if you don't, then what's the point of this? You can't get your numbers back. Perhaps you would rather use strings instead of integers.
Nope. Basically, just do the arithmetic:
int digitConcatenate(int prefix, int suffix, int base = 10) { int counter = suffix; int shifted = prefix; do { shifted *= base; counter /= base; } while counter; return shifted + suffix;}
I don't need to be able to get the numbers back from that xyz (or whatever) number.
It's not pre-determined how many digits they are supposed to be. I could of course do this with if statements so that I wouldn't need to know how many digits they are supposed to be but that is the 'bad way' that I mentioned.
It's not pre-determined how many digits they are supposed to be. I could of course do this with if statements so that I wouldn't need to know how many digits they are supposed to be but that is the 'bad way' that I mentioned.
Well, here's a function:
It doesn't do anything interesting with negative numbers, but then again I'm not really sure what appending -5 and -4 would be supposed to yeild.
Why you would need such a function beyond me, so back to my question, what's the point of this?
#include <cmath>int appendTo(int a, int b) { return b + a * static_cast<int>(std::pow(10.0, 1.0+std::floor(std::log10(static_cast<double>(b))))); }
It doesn't do anything interesting with negative numbers, but then again I'm not really sure what appending -5 and -4 would be supposed to yeild.
Why you would need such a function beyond me, so back to my question, what's the point of this?
The safe C++ way is to use:
An unsafe C-only alternative is:
#include <sstream>std::stringstream ss;ss << x << y << z;int result;ss >> result;return( result );// Or you could directly return a string:return ss.str( );
An unsafe C-only alternative is:
buffer[256];sprintf( buffer, "%i%i%i", x, y, z ); //Unsafe, can overflow!return( atoi( buffer ) );
Quote:Original post by smart_idiotOn the other hand snprintf isn't standard and would silently cut of the string if additional buffer space were required.
sprintf is a safety hazard, use snprintf instead.
A better solution would be to approximate the maximum required buffer size instead.
enum { MAX_DIGITS = sizeof(unsigned) * CHAR_BIT / 3 };char buf[MAX_DIGITS * 2 + 1];
However the arithmetic solution is probably preferable..
Quote:Original post by doynax
On the other hand snprintf isn't standard
[begin nit-pick]
Actually it is but its part of the C99 standard and C++ 0x03 TR1 standard ( std::tr1::snprintf ).
[end nit-pick]
Quote:Original post by snk_kidYeah, I guess you're right.
Actually it is but its part of the C99 standard and C++ 0x03 TR1 standard ( std::tr1::snprintf ).
I should've written "snprintf() isn't portable in practice" instead.
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement