C ++ equivalent to Python len () function?

I have an integer and need to find out how many digits are in it.

+6
c ++
source share
9 answers

It is a little difficult to handle negative numbers and the case where the input is zero:

int length(int n) { int len = 0; if (n < 0) { len = 1; n = -n; } while (n > 9) { n /= 10; len++; } return len+1; } 
+4
source share

For positive numbers, use log10 :

 int a = 1234; int len = static_cast<int>(log10(a)+1.); 

If you need to be careful:

 int length(int a) { int b = abs(a); if (b == 0) return 1; return static_cast<int>(log10(b)+1.); } 

With that said, it would be better to do a repeated division by 10 in practice.

 int length(int a) { int b = 0; for (a = abs(a); a != 0; b++, a /= 10) continue; return b; } 
+10
source share

You probably mean that you have a string containing numbers, not an int in python:

 >>> i = 123456789 >>> len(i) Traceback (most recent call last): File "<console>", line 1, in <module> TypeError: object of type 'int' has no len() >>> len(str(i)) 9 

If this is the case in C ++, it is easy to find the length of the string using:

 my_str_value.length() 

or for string C using strlen

+7
source share

There is no such function in the C ++ library. However, for simplicity you can use std::stringstream .

Try this (processes negative numbers as well).

  int a =-12345,x; x = std::abs(a) std::stringstream s; s << x; std::cout<<s.str().size(); 
+6
source share

Hmm ... Python:

 >>> len(5) Traceback (most recent call last): File "<pyshell#45>", line 1, in <module> len(5) TypeError: object of type 'int' has no len() 

Not what you wanted?

ok, let's assume you have an actual integer. base 10 of the log will tell you what you want to know numerically, that is, if yournumber == pow(10, digits) , then log10(yournumber) == digits ! unfortunately, if your number is not an exact capacity of 10, you will have a fraction to deal with. It's easy enough to handle the floor() function, which is simply rounded. beware of negative numbers, as the logarithms are undefined in real numbers for non-positive values.

 #include <iostream> #include <math.h> int main() { std::cout << floor(log10(5))+1 << std::endl; std::cout << floor(log10(30))+1 << std::endl; std::cout << floor(log10(2000))+1 << std::endl; std::cout << floor(log10(16000))+1 << std::endl; } 

we have to add 1, because 10 to 1 is still 10, so we are disabled by one. Add one to the exponent and you have the numbers!

+5
source share

You must continue to divide it by 10 (assuming it is an integer). You do this because you delete a digit every time the loop repeats.

something like:

 int number; int digits; while (number > 0) { digits++; number /= 10; } 

You probably want to make sure that the number starts from zero.

+2
source share
 int intlen(float num) { int cnt = 0; while(num >= 1) { num = num / 10; cnt++; } return cnt; } 
+2
source share

Here is a small example:

 int numberDigits(int n) { char buffer[100]; itoa(n,buffer,10); int len=0; while (buffer[len]!=0) { len++; } return len; } 
0
source share

Ignoring the point where len() in python returns the number of elements in a sequence rather than the number of digits in integers, here is a function for counting the number of digits in integers without division (so this should be much faster than similar solutions that use division).

 int number_of_digits(int value) { int count = 0; int i = 1; if (value < 0) { value *= -1; } while (i < value) { count++; i *= 10; } if (count > 0) { return count; } else { return 1; } } 

For extra speed, you can even replace the multiplication with ten with some bit:

 i = ((i << 2) + i) << 1; 

(A bit shift is cool, but multiplication can be "free" if your processor can multiply the pipeline by some unused multiplication block - modern processors are a thing of beauty).

0
source share

All Articles