C++ 将整数转换为数组
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1860983/
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
Convert integer to array
提问by kantor
I would like to convert an integer into an array, so that it looks like the following:
我想将一个整数转换为一个数组,使其如下所示:
int number = 123456 ;
int array[7] ;
with the result:
结果:
array[0] = 1
array[1] = 2
...
array[6] = 6
回答by Broam
Perhaps a better solution is to work backwards:
也许更好的解决方案是向后工作:
123456 % 10 = 6
123456 % 10 = 6
123456 / 10 = 12345
123456 / 10 = 12345
12345 % 10 = 5
12345 % 10 = 5
12345 / 10 = 1234
12345 / 10 = 1234
回答by user224003
just use modular arithmetic:
只需使用模算术:
int array[6];
int number = 123456;
for (int i = 5; i >= 0; i--) {
array[i] = number % 10;
number /= 10;
}
回答by Orhun Yeldan
Here what I came up with, the integerToArray function returns a vector that is converted from the integer value. you can test it with the main function as well:
在这里我想出了, integerToArray 函数返回一个从整数值转换而来的向量。您也可以使用 main 函数对其进行测试:
#include <iostream>
#include <vector>
using namespace std;
vector <int> integerToArray(int x)
{
vector <int> resultArray;
while (true)
{
resultArray.insert(resultArray.begin(), x%10);
x /= 10;
if(x == 0)
return resultArray;
}
}
int main()
{
vector <int> temp = integerToArray(1234567);
for (auto const &element : temp)
cout << element << " " ;
return 0;
}
//outputs 1 2 3 4 5 6 7
回答by avakar
You can extract the last digit of the number this way:
您可以通过以下方式提取数字的最后一位数字:
int digit = number % 10;
number /= 10;
Note that you should also check whether number
is positive. Other values require additional handling.
请注意,您还应该检查是否number
为正。其他值需要额外处理。
回答by philsquared
Take the log10 of the number to get the number of digits. Put that in, say pos
, then, in a loop, take the modulo of 10 (n % 10
), put the result in the array at position pos
. Decrement pos
and divide the number by 10. Repeat until pos == 0
取数字的 log10 得到位数。把它放在,比如说pos
,然后,在一个循环中,取 10 ( n % 10
)的模,把结果放在数组中的位置pos
。pos
将数字递减并除以 10。重复直到pos == 0
What did you want to do with the sign if it's negative?
如果它是负数,你想用它做什么?
回答by jalf
You can't simply "convert" it. The integer is not represented in software in decimal notation. So the individual digits you want don'texist. They have to be computed.
你不能简单地“转换”它。整数在软件中不以十进制表示法表示。所以你想要的单个数字不存在。它们必须被计算。
So, given an arbitrary number, how can you determine the number of ones?
那么,给定一个任意数字,你如何确定一个的数量?
We could divide by ten, and then take the remainder: For 123, the division would give 12, and then there's a remainder of 3. So we have 3 ones. The 12 tells us what we have past the ones, so it can be our input for the next iteration. We take that, divide by 10, and get 1, and a remainder of 2. So we have 2 in the tens place, and 1 left to work with for the hundreds. Divide that by 10, which gives us zero, and a remainder of 1. So we get 1 in the hundreds place, 2 in the tens place, and 3 in the ones place. And we're done, as the last division returned zero.
我们可以除以 10,然后取余数:对于 123,除法会得到 12,然后剩下 3。所以我们有 3 个。12 告诉我们过去的那些,所以它可以作为我们下一次迭代的输入。我们把它除以 10,得到 1,余数为 2。所以我们在十位上有 2,剩下的 1 可以用于数百位。将其除以 10,得到 0,余数为 1。所以我们在百位得到 1,在十位得到 2,在个位得到 3。我们完成了,因为最后一个除法返回零。
回答by Peter Olsson
See SO question Language showdown: Convert string of digits to array of integers?for a C/C++ version (as well as other languages).
请参阅 SO 问题语言摊牌:将数字字符串转换为整数数组?对于 C/C++ 版本(以及其他语言)。
回答by Dominic.wig
if this is really homework then show it your teacher - just for fun ;-)
如果这真的是家庭作业,那么把它展示给你的老师——只是为了好玩;-)
CAUTION! very poor performance, clumsy way to reach the effect you expect and generally don't do this at home(work) ;-)
警告!性能很差,笨拙的方式来达到你期望的效果,一般不要在家里(工作)这样做;-)
#include <algorithm>
#include <iostream>
#include <sstream>
#include <string>
#include <vector>
typedef std::vector< int > ints_t;
struct digit2int
{
int operator()( const char chr ) const
{
const int result = chr - '0';
return result;
}
};
void foo( const int number, ints_t* result )
{
std::ostringstream os;
os << number;
const std::string& numberStr = os.str();
std::transform(
numberStr.begin(),
numberStr.end(),
std::back_inserter( *result ),
digit2int() );
}
int main()
{
ints_t array;
foo( 123456, &array );
std::copy(
array.begin(),
array.end(),
std::ostream_iterator< int >( std::cout, "\n" ) );
}
回答by smokenstein
If you wanted to turn it into a string then it would be really easy, just do what everyone else is saying about using the % operator:
如果你想把它变成一个字符串,那么它真的很容易,只要按照其他人所说的使用 % 运算符:
Let's say num = 123, we can do this:
假设 num = 123,我们可以这样做:
string str;
while (num > 0)
{
str = (num % 10) + str; //put last digit and put it into the beginning of the string
num = num /10; //strip out the last digit
}
Now you can use str as an array of chars. Doing this with an array is a hassle because putting things in the beginning of an array requires you to shift everything else. What we can do is, instead of putting each digit into a string, we can put it into a stack. It will put it in a backwards order like this: 3 2 1. Then we can pop off the top number one by one and put that into an array in the correct order. You array will look like this: 1 2 3. I will leave the implementation to you since this is homework.
现在您可以将 str 用作字符数组。用数组做这件事很麻烦,因为把东西放在数组的开头需要你移动其他所有东西。我们可以做的是,与其将每个数字放入一个字符串中,不如将其放入一个堆栈中。它将按照这样的倒序排列:3 2 1。然后我们可以一个一个地弹出顶部的数字,并以正确的顺序将其放入一个数组中。您的数组将如下所示:1 2 3。我将把实现留给您,因为这是家庭作业。
@Broam has a good solution, but like he stated, it's for working backwards. I think the OP or whoever comes looking into this thread will want it forwards and that's why I'm posting this. If you have a better solution, please reply, I'm interested as well.
@Broam 有一个很好的解决方案,但正如他所说,它是为了向后工作。我认为 OP 或任何查看此线程的人都会希望它转发,这就是我发布此内容的原因。如果您有更好的解决方案,请回复,我也很感兴趣。
回答by Drew Dormann
You can use modulus to determine the last digit.
您可以使用模数来确定最后一位数字。
And you can use division to move another digit to the last digit's place.
您可以使用除法将另一个数字移动到最后一个数字的位置。