Home > Software engineering >  How I compare the two strings of digits of size >=100 ? Because on using stoi and stoll giving me
How I compare the two strings of digits of size >=100 ? Because on using stoi and stoll giving me

Time:07-18

How I compare the two strings of digits of size 100 ? Because on using stoi and stoll giving me overflow RUNTIME ERROR

terminate called after throwing an instance of 'std::out_of_range' what(): stoll

str1="1234567894561236548752................."
str2="367869446879446589646..................."

how i compare it which is smaller /greater?

CodePudding user response:

You don't need to convert the strings to integers to compare.

First, you can compare the signs of the strings. If one is non-negative and another is negative, it is clear that the non-negative is greater than the negative.

Then we can compare the length of string. (Assuming both of the strings are integers. If the string contains floating point, then we compare the length of integral part). The longer string will be greater than the shorter one if two strings are both non-negative. If two strings are both negative, then the longer one is smaller.

If two strings have the same length of integral part, then we can use a for loop, to compare the digits from left to right (from highest to lowest).

  • Related