### string and numbrs.

if I'm dealing with a very large number

for example string temp ="012345";

but my program need to change the value in certain condition such as

temp[3] = temp[3]+1;

but i guess this is not working.

any one knows other methods for string to accomplish this goal?
 but i guess this is not working.

This "works", although it depends on what you're trying to accomplish.

 to accomplish this goal?

If you want to deal with large numbers, use GMP.
Last edited on
 temp[3] = temp[3]+1; but i guess this is not working.

It is most certanly working, but unly untill you hit 10.
An obvious (though not efficient) way would be to simulate digit by digit addition with carry.

Something like:
 ``123456789101112131415161718192021222324252627282930313233343536373839`` ``````constexpr char ZERO = '0', NINE = '9' ; inline bool is_unsigned_number( const std::string& number ) { for( char c : number ) if( c < '0' || c > '9' ) return false ; return !number.empty() ; } std::string operator+ ( const std::string& number, unsigned int value ) { assert( is_unsigned_number(number) ) ; constexpr int TEN = 10 ; std::string result ; result.reserve( number.size() + 2 ) ; for( auto iter = number.rbegin() ; iter != number.rend() ; ++iter ) { int digit = *iter - ZERO + value ; value = digit / TEN ; result = char( digit % TEN + ZERO ) + result ; } while( value > 0 ) { result = char( value % TEN + ZERO ) + result ; value = value / TEN ; } return result ; } inline std::string operator+ ( unsigned int value, const std::string& number ) { return number + value ; } int main() { std::string n = "12345" ; std::cout << n + 999999U << ' ' << 1234U + n << '\n' ; }``````

For real life code:
 If you want to deal with large numbers, use GMP.
This library is awesome and does exactly what you are looking for:

http://gmplib.org/
GMP or MPIR, depending of wahat compiler are you using. In windows GMP does not compile with Visual Studio, you must use MSYS and MinGW.
I'm using Xcode. and working an UVA problems.
is online judge accept the GMP lib?
Last edited on
I haven't looked through the UVa site, but I doubt that GMP will be accepted.

I also don't think you need it. Remember, '0'..'9' is the same as 0..9, except that, for example, '7' is the same as '0'+7.
Topic archived. No new replies allowed.