重庆分公司,新征程启航
为企业提供网站建设、域名注册、服务器等服务
gcc 编译测试通过
目前创新互联建站已为上千多家的企业提供了网站建设、域名、网站空间、网站托管、企业网站设计、抚远网站维护等服务,公司将坚持客户导向、应用为本的策略,正道将秉承"和谐、参与、激情"的文化,与客户和合作伙伴齐心协力一起成长,共同发展。
#include stdlib.h
#include stdio.h
#define N 10
int * copyArray(int *source, int n)
{
int *dest;
int i;
// 分配空间
dest = (int*)malloc(n * sizeof(int));
// 顺序复制
for(i = 0;i n;i ++)
dest[i] = source[i];
return dest;
}
int *copyReverse(int *source, int n)
{
int *dest;
int i;
// 分配空间
dest = (int*)malloc(n * sizeof(int));
// 逆序复制
for(i = 0;i n;i ++)
dest[n - i - 1] = source[i];
return dest;
}
int *copyOrder(int *source, int n)
{
int *dest;
int i,j,minIndex;
// 分配空间
dest = (int*)malloc(n * sizeof(int));
// 顺序复制
for(i = 0;i n;i ++)
dest[i] = source[i];
// 对数组选择排序
for(i = 0;i n - 1;i ++)
{
minIndex = i;
for(j = i;j n;j ++)
{
// 选择本次最小下标(如果需要降序,将 改为 ,重新编译)
if(dest[j] dest[minIndex])
minIndex = j;
// 交换元素
if(minIndex != i)
{
dest[i] = dest[i] ^ dest[minIndex];
dest[minIndex] = dest[i] ^ dest[minIndex];
dest[i] = dest[i] ^ dest[minIndex];
}
}
}
return dest;
}
int main()
{
int test[N] = {2,4,1,0,9,5,6,8,7,3};
int *origin,*reverse,*order;
int i;
origin = copyArray(test,N);
reverse = copyReverse(test,N);
order = copyOrder(test,N);
for(i = 0; i N; i ++)
printf("%d ",origin[i]);
printf("\n");
for(i = 0; i N; i ++)
printf("%d ",reverse[i]);
printf("\n");
for(i = 0; i N; i ++)
printf("%d ",order[i]);
printf("\n");
free(origin);
free(reverse);
free(order);
return 0;
}
有两种常用的方法。
1 对数组各个维循环,遍历每个元素,并将其赋值到目标数组的对应位置上。
缺点:代码相对复杂。
优点:可以不不同大小和形式的数组进行交叉复制。
2 利用C语言中多维数组元素存储连续性,使用memcpy函数整体复制。
缺点:仅使用源数组要复制的数据是连续的,同时在目标数组中以同样顺序连续复制的情况。
优点:代码简单,一个函数调用即可完成赋值。相对第一种,执行效率略高。
在日常编程过程中,我们可能经常需要Copy各种数组,一般来说有以下几种常见的方法:Array.Copy,IListT.Copy,BinaryReader.ReadBytes,Buffer.BlockCopy,以及System.Buffer.memcpyimpl,由于最后一种需要使用指针,所以本文不引入该方法。
本次测试,使用以上前4种方法,各运行1000万次,观察结果。
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
namespace BenchmarkCopyArray
{
class Program
{
private const int TestTimes = 10000000;
static void Main()
{
var testArrayCopy = new TestArrayCopy();
TestCopy(testArrayCopy.TestBinaryReader, "Binary.ReadBytes");
TestCopy(testArrayCopy.TestConvertToList, "ConvertToList");
TestCopy(testArrayCopy.TestArrayDotCopy, "Array.Copy");
TestCopy(testArrayCopy.TestBlockCopy, "Buffer.BlockCopy");
Console.Read();
}
private static void TestCopy(Action testMethod, string methodName)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
for (int i = 0; i TestTimes; i++)
{
testMethod();
}
testMethod();
stopWatch.Stop();
Console.WriteLine("{0}: {1} seconds, {2}.", methodName, stopWatch.Elapsed.Seconds, stopWatch.Elapsed.Milliseconds);
}
}
class TestArrayCopy
{
private readonly byte[] _sourceBytes = new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 };
public void TestBinaryReader()
{
var binaryReader = new BinaryReader(new MemoryStream(_sourceBytes));
binaryReader.ReadBytes(_sourceBytes.Length);
}
public void TestConvertToList()
{
IListbyte bytesSourceList = new Listbyte(_sourceBytes);
var bytesNew = new byte[_sourceBytes.Length];
bytesSourceList.CopyTo(bytesNew, 0);
}
public void TestArrayDotCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Array.Copy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
public void TestBlockCopy()
{
var bytesNew = new byte[_sourceBytes.Length];
Buffer.BlockCopy(_sourceBytes, 0, bytesNew, 0, _sourceBytes.Length);
}
}
}
#include"string.h"
#include"stdio.h"
intmain(void)
{
inti,j;
inta[2][3]={{1,2,3},{4,5,6}};
intb[2][3];
memcpy(b[0][0],a[0][0],24);
printf("%d",b[1][0]);
}
扩展资料
#includestdio.h
#includestring.h
#includestdlib.h
voidprintarr2d(int(*a)[3],introw,intcol);
intmain()
{
inti,j;
inta[2][3]={{1,2,3},{4,5,6}};
intb[4][3]={{0,0,0},{0,0,0}};
memcpy(b[2],a,sizeof(int)*2*3);
printarr2d(b,4,3);
return0;
}
/***********************************************
打印显示数组
************************************************/
voidprintarr2d(int(*a)[3],introw,intcol)
{
inti,j;
for(i=0;irow;i++)
{
for(j=0;jcol;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}
}
C++ 复制函数
一、场景一:全部默认
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt){
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:public base
{
public:
derived()
{
}
derived(int dt,int ddt):
base(dt),ddata(ddt)
{
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};
int main()
{
derived da(1,5);
derived db(2,10);
da.get();
db.get();
da = db;
da.get();
db.get();
derived dc(da);
dc.get();
base ba(da);
ba.get();
getchar();
}
场景二、基类默认,派生类自定义
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt)
{
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:
public base
{
public:
derived()
{
}
derived(int dt,int ddt)
:base(dt),ddata(ddt)
{
}
derived(const derivedd)
{
printf("call derived::copy construct function.\n");
ddata = d.ddata;
}
derived operator=(const derivedd)
{
printf("call derived::operator= function.\n");
ddata = d.ddata;
return *this;
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};
场景三、基类自定义,派生类默认
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt)
{
}
base(const baseb)
{
printf("call base::copy construct function.\n");
data = b.data;
}
base operator=(const based)
{
printf("call base::operator= function.\n");
data = d.data;
return *this;
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:public base
{
public:
derived()
{
}
derived(int dt,int ddt):
base(dt),ddata(ddt)
{
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};
场景四、基类派生类都自定义
#include
class base
{
public:
base()
{
}
base(int dt):
data(dt)
{
}
base(const baseb)
{
printf("call base::copy construct function.\n");
data = b.data;
}
base operator=(const based)
{
printf("call base::operator= function.\n");
data = d.data;
return *this;
}
void get()
{
printf("base::data = %d\n",data);
}
public:
int data;
};
class derived:public base
{
public:
derived()
{
}
derived(int dt,int ddt):
base(dt),
ddata(ddt)
{
}
derived(const derivedd):base(d)
{
printf("call derived::copy construct function.\n");
ddata = d.ddata;
}
derived operator=(const derivedd)
{
printf("call derived::operator= function.\n");
ddata = d.ddata;
return *this;
}
void get()
{
base::get();
printf("derived::ddata = %d\n",ddata);
}
private:
int ddata;
};