如果您正在寻找一种整洁的复制方式。我建议你定义一个函数这是在给定位置复制的类似 STL 的实现
#include<vector>
#include<cstdio>
using namespace std;
/**
* Copies the elements all elements of B to A at given positions.
* indexing array ranges [ind_first, ind_lat)
* e.g
* int A[]= {0, 2, 10, 7, 1, 3, 6, 10, 10, 2, 10};
* int B[] = {-1, -1, -1, -1};
* int ind[] = {10, 5, 9, 2};
* copyat(B, A, &ind[0], &ind[3]);
* // results: A:[0,2,10,7,1,-1,6,10,10,-1,-1]
*/
template<class InputIterator, class OutputIterator, class IndexIterator>
OutputIterator copyat (InputIterator src_first,
OutputIterator dest_first,
IndexIterator ind_first,
IndexIterator ind_last)
{
while(ind_first!=ind_last) {
*(dest_first + *ind_first) = *(src_first);
++ind_first;
}
return (dest_first+*ind_first);
}
int main()
{
int A[]= {0, 2, 10, 7, 1, 3, 6, 10, 10, 2, 10};
int B[] = {-1, -1, -1, -1};
int ind[] = {10, 5, 9, 2};
copyat(B, A, &ind[0], &ind[3]);
printf("A:[");
for(int i=0; i<10; i++)
printf("%d,", A[i]);
printf("%d]\n",A[10]);
}