我有以下问题:
使用一个名为"merge“的函数编写一个程序,该函数将一个数组的数据整数复制到一个较大的数组中,然后在第一个数组的内容之后将第二个数组的数据整数复制到较大的数组中。
我的功能有问题
如果我为数组1输入{1,2},为数组2输入{3,4}
然后输出为12 -57574 -658675。
应该是1 2 3 4
void merge (int a[], int n, int b[],int m) {
int c[100];
int x=n+m ; //size of merge aray c[]
for(int i = 0; i < n; i++)
c[i]=a[i];
for(int j = n ; j < x ; j++)
c[j] = b[j];
cout<<endl<<endl;
for(int k = 0; k < x; k++)
cout<<c[k]<<" ";
}发布于 2012-04-13 10:11:25
问题:
动态创建大小合适的数组所需的b[n].
发布于 2012-09-07 22:36:56
这是我的简单密码。修改它以达到合并功能的目的。
int main() {
int a[5];
int b[5];
int c[10];
cout << "Enter elements for array a[5]:" << endl;
int i = 0;
do {
cin >> a[i];
i++;
} while (i <= 4);
cout << "Enter elements for array b[5]:" << endl;
i = 0;
do {
cin >> b[i];
i++;
} while (i <= 4);
for (register int x = 0; x <= 5; x++) {
if (x == 5) {
for (register int h = 0; h < 5; h++) {
c[x] = b[h];
x++;
}
break;
}
c[x] = a[x];
}
for (register int x = 0; x < 10; x++) {
cout << c[x] << " ";
}
return (0);
}发布于 2012-04-13 10:52:18
为什么不直接用向量呢?就像这样:
std::vector<int> concat(const std::vector<int>& a, const std::vector<int>& b) {
std::vector<int> c;
c.reserve(a.size() + b.size());
c.insert(c.end(), a.begin(), a.end());
c.insert(c.end(), b.begin(), b.end());
return c;
}https://stackoverflow.com/questions/10138918
复制相似问题