053:指针练习:SwapMemory
总时间限制: 1000ms 内存限制: 65536kB
描述
填写内存交换函数 SwapMemory,使得程序输出指定结果
#include <iostream>
using namespace std;
void SwapMemory(void * m1,void * m2, int size)
{
// 在此处补充你的代码
}
void PrintIntArray(int * a,int n)
{
for(int i = 0;i < n; ++i)
cout << a[i] << ",";
cout << endl;
}
int main()
{
int a[5] = {1,2,3,4,5};
int b[5] = {10,20,30,40,50};
SwapMemory(a,b,5 * sizeof(int));
PrintIntArray(a,5);
PrintIntArray(b,5);
char s1[] = "12345";
char s2[] = "abcde";
SwapMemory(s1,s2,5);
cout << s1 << endl;
cout << s2 << endl;
return 0;
}
输入
无
输出
10,20,30,40,50,
1,2,3,4,5,
abcde
12345
样例输入
无
样例输出
10,20,30,40,50,
1,2,3,4,5,
abcde
12345
来源
Guo Wei
代码
#include <iostream>
using namespace std;
void SwapMemory(void * m1,void * m2, int size)
{// 在此处补充你的代码
char *m11=(char *)m1;
char *m22=(char *)m2;
while(size--){
*(m11+size)=(*(m22+size))^(*(m11+size));
*(m22+size)=(*(m22+size))^(*(m11+size));
*(m11+size)=(*(m22+size))^(*(m11+size));}
}
void PrintIntArray(int * a,int n)
{
for(int i = 0;i < n; ++i)
cout << a[i] << ",";
cout << endl;
}
int main()
{
int a[5] = {1,2,3,4,5};
int b[5] = {10,20,30,40,50};
SwapMemory(a,b,5 * sizeof(int));
PrintIntArray(a,5);
PrintIntArray(b,5);
char s1[] = "12345";
char s2[] = "abcde";
SwapMemory(s1,s2,5);
cout << s1 << endl;
cout << s2 << endl;
return 0;
}
注意:m1和m2可能地址有冲突,交换值要从高位到低位