【发布时间】:2012-12-25 23:05:00
【问题描述】:
我想创建一个将 int 分隔为 char 数组的 C++ 方法。 并返回 int 的一部分。
示例:
输入:
int input = 11012013;
cout << "day = " << SeperateInt(input,0,2); << endl;
cout << "month = " << SeperateInt(input,2,2); << endl;
cout << "year = " << SeperateInt(input,4,4); << endl;
输出:
day = 11
month = 01
year = 2013
我认为它类似于this。但这对我不起作用,所以我写道:
int separateInt(int input, int from, int length)
{
//Make an array and loop so the int is in the array
char aray[input.size()+ 1];
for(int i = 0; i < input.size(); i ++)
aray[i] = input[i];
//Loop to get the right output
int output;
for(int j = 0; j < aray.size(); j++)
{
if(j >= from && j <= from+length)
output += aray[j];
}
return output;
}
但是,
1) 你不能用这种方式调用 int 的大小。
2) 你不能像一个字符串一样说我想要元素 i int的,因为那样的话这个方法就没用了
如何解决?
【问题讨论】:
-
input.size()其中input是int!!! -
你确定你的输入不是字符串吗?