连接数组,数组1内存消失了C ++

时间:2017-03-21 03:53:50

标签: c++ arrays concatenation

程序由输入文件中的array1 [20]和array2 [40]组成。然后将Array1添加到array2的末尾(之后它们将按时间顺序排序,但我还没有到达那里,但我仍然坚持这个)。最终结果将被打印出来;我没有包含这部分代码,也没有包含输入文件的代码,以便您更容易阅读:)感谢您的帮助。

代码没有错误。两个输入文件的输入数字为1到20,输出读数为

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 135062784 135305664 134511120 135308884 6 0 6 16 135303844 134511520 134511136 134957510 135308884 135111932 135120188 0 134511156 135303952 134511168 134805750

int readSortedArray (int array[20], int count, istream& infile)
{   
count = 0;
while( (count < 20) && (infile >> array[count]) )
{
    count++;
}

return count;
}

int main ()
{
 int array[20], array1[20], array2[40];
 int count, count1, count2, x;

count1 = readSortedArray(array1, count1, infile1);
count2 = readSortedArray(array2, count2, infile2);

 for (int i=1; i < count1 + 1; i++ )
   {
       array1[i - 1] >> array2[count2 + i];
   }

 return 0;
}

1 个答案:

答案 0 :(得分:0)

似乎main函数中的for循环应该是这个(我应该从0开始)。并确保infile2真正包含您认为它包含的内容。

for (int i=0; i < count1; i++ )
{
  array2[count2 + i] = array1[i];
}