C++ 将整数转换为数字数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15987666/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Converting integer into array of digits
提问by user2252786
I need to convert two integers into two arrays of digits, so for example 544 would become arr[0] = 5, arr[1] = 4, arr[2] = 4
.
我需要将两个整数转换为两个数字数组,例如 544 将变为arr[0] = 5, arr[1] = 4, arr[2] = 4
.
I have found some algorithms doing this, but they create new array, and return this. I would have to allocate this memory for two arrays, so I wanna pass two integers by reference and do this on them directly.
我发现一些算法会这样做,但它们会创建新数组并返回它。我必须为两个数组分配这个内存,所以我想通过引用传递两个整数并直接对它们执行此操作。
I guess I can do this, because these integers are in fact template types, so they should be changeable. That's why I added C++ tag here.
我想我可以做到这一点,因为这些整数实际上是模板类型,所以它们应该是可变的。这就是我在这里添加 C++ 标签的原因。
回答by Mario
Just using something like this:
只是使用这样的东西:
int n = 544; // your number (this value will Change so you might want a copy)
int i = 0; // the array index
char a[256]; // the array
while (n) { // loop till there's nothing left
a[i++] = n % 10; // assign the last digit
n /= 10; // "right shift" the number
}
Note that this will result in returning the numbers in reverse order. This can easily be changed by modifying the initial value of i
as well as the increment/decrement based on how you'd like to determine to length of the value.
请注意,这将导致以相反的顺序返回数字。这可以通过修改初始值i
以及基于您想要确定值长度的增量/减量来轻松更改。
(Brett Hale) I hope the poster doesn't mind, but I thought I'd add a code snippet I use for this case, since it's not easy to correctly determine the number of decimal digits prior to conversion:
(布雷特黑尔)我希望海报不介意,但我想我会添加一个我用于这种情况的代码片段,因为在转换之前正确确定小数位数并不容易:
{
char *df = a, *dr = a + i - 1;
int j = i >> 1;
while (j--)
{
char di = *df, dj = *dr;
*df++ = dj, *dr-- = di; /* (exchange) */
}
}
回答by masoud
A simple solution is:
一个简单的解决方案是:
int i = 12312278;
std::vector<int> digits;
while (i)
{
digits.push_back(i % 10);
i /= 10;
}
std::reverse(digits.begin(), digits.end());
or, string based ( i >= 0 )
或者,基于字符串( i >= 0 )
for (auto x : to_string(i))
digits.push_back(x-'0');
回答by Patashu
Call the integer a
.
调用整数a
。
To get the units digit of a
, a % 10
要获得 的个位数a
,a % 10
To shift a
down so the tens is the units digit, a / 10
要a
向下移动,所以十位是个位数,a / 10
To know when you're done, a == 0
要知道你什么时候完成, a == 0
To know how large your array needs to be in the first place, min(ceil(log(a+1, 10)), 1)
(to convince yourself this works, try the logarithm part of it in a calculator. if you don't have multiple argument log, use the identity log(x,y) == log(x)/log(y)
)
首先要知道您的数组需要多大,min(ceil(log(a+1, 10)), 1)
(为了说服自己这是有效的,请在计算器中尝试它的对数部分。如果您没有多个参数日志,请使用身份log(x,y) == log(x)/log(y)
)
回答by Johnny Mnemonic
You can do something like this if you are using C++:
如果您使用 C++,您可以执行以下操作:
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
int a=544;
stringstream str;
str << a;
string arr;
str>>arr;
for(int i=0; i<arr.length(); i++)
{
cout << arr[i];
}
system("pause");
return 0;
}