在Java中,我使用
int length = String.valueOf(input).length();
Run Code Online (Sandbox Code Playgroud)
找到整数的长度.
我的问题是:在C++中有没有类似的方法?我已经尝试过for循环和while循环,例如:
while (input > 0){
input/=10;
count++;
Run Code Online (Sandbox Code Playgroud)
因此,除了循环之外,C++中还有其他任何可用的东西.谢谢您的回答.
如果您想要用Java编写的完全对应物,可以使用:
int length = to_string(input).length();
Run Code Online (Sandbox Code Playgroud)
请注意,这to_string是一个C++ 11功能.另外,要小心负数.