#include<iostream>
using namespace std;
//void mycopy(int* dst,int* src,int* src_end)
//{
// for(int* p=src;p<=src_end;p++)
// {
// *dst++ = *p;
// }
//}
//void mycopy(int* dst,int* src,int n)
//{
// for(int i=0;i<n;i++)
// {
// dst[i]=src[i];
// }
//}
void show(int *a,int n)
{
for(int i=0;i<n;i++)
{
cout << a[i] << " ";
cout << endl;
}
}
void mycopy(int* dst,int* src,int* src_end)
{
int n=src_end-src;
if(dst==src) return;
if(dst>src)
{
for(int i=n-1;i>=0;i--)
{
dst[i+(dst-src)]=src[i];
}
} else{
for(int i=0;i<n;i++) dst[i+(dst-src)]=src[i];
}
}
int main()
{
int a[]={1,2,3,4,5};
int b[]={10,20,30,40,50};
mycopy(b+3,a+2,a+2+2);
show(b,6);
return 0;
}
输出:
10
20
30
40
50
0
按照你说的改了,也不是10,20,30,3,4,5,而且这回我也没太明白为什么要改为dst[i+(dst-src)]