正如其他人所提到的,在 C 语言中您将使用 memcpy。但是请注意,这是一个原始内存复制,所以如果您的数据结构有指向它们自己或彼此的指针,那么复制中的指针仍然指向原始对象。
在 C + + 中,如果你的数组成员是 POD,你也可以使用 memcpy(也就是说,本质上的类型,你也可以在 C 中使用不变) ,但是一般来说,memcpy是允许使用 没有的。正如其他人提到的,要使用的函数是 std::copy。
Having said that, in C++ you rarely should use raw arrays. Instead you should either use one of the standard containers (std::vector is the closest to a built-in array, and also I think the closest to Java arrays — closer than plain C++ arrays, indeed —, but std::deque or std::list may be more appropriate in some cases) or, if you use C++11, std::array which is very close to built-in arrays, but with value semantics like other C++ types. All the types I mentioned here can be copied by assignment or copy construction. Moreover, you can "cross-copy" from opne to another (and even from a built-in array) using iterator syntax.
这给出了可能性的概述(我假设所有相关的标题都包括在内) :
int main()
{
// This works in C and C++
int a[] = { 1, 2, 3, 4 };
int b[4];
memcpy(b, a, 4*sizeof(int)); // int is a POD
// This is the preferred method to copy raw arrays in C++ and works with all types that can be copied:
std::copy(a, a+4, b);
// In C++11, you can also use this:
std::copy(std::begin(a), std::end(a), std::begin(b));
// use of vectors
std::vector<int> va(a, a+4); // copies the content of a into the vector
std::vector<int> vb = va; // vb is a copy of va
// this initialization is only valid in C++11:
std::vector<int> vc { 5, 6, 7, 8 }; // note: no equal sign!
// assign vc to vb (valid in all standardized versions of C++)
vb = vc;
//alternative assignment, works also if both container types are different
vb.assign(vc.begin(), vc.end());
std::vector<int> vd; // an *empty* vector
// you also can use std::copy with vectors
// Since vd is empty, we need a `back_inserter`, to create new elements:
std::copy(va.begin(), va.end(), std::back_inserter(vd));
// copy from array a to vector vd:
// now vd already contains four elements, so this new copy doesn't need to
// create elements, we just overwrite the existing ones.
std::copy(a, a+4, vd.begin());
// C++11 only: Define a `std::array`:
std::array<int, 4> sa = { 9, 10, 11, 12 };
// create a copy:
std::array<int, 4> sb = sa;
// assign the array:
sb = sa;
}
我在这里给出两种应对数组的方法,用于 C 和 C + + 语言。Memcpy和 收到都可以在 C + + 中使用,但是 收到不能在 C 中使用,如果你想在 C 中复制数组,你必须使用 Memcpy。
#include <stdio.h>
#include <iostream>
#include <algorithm> // for using copy (library function)
#include <string.h> // for using memcpy (library function)
int main(){
int arr[] = {1, 1, 2, 2, 3, 3};
int brr[100];
int len = sizeof(arr)/sizeof(*arr); // finding size of arr (array)
std:: copy(arr, arr+len, brr); // which will work on C++ only (you have to use #include <algorithm>
memcpy(brr, arr, len*(sizeof(int))); // which will work on both C and C++
for(int i=0; i<len; i++){ // Printing brr (array).
std:: cout << brr[i] << " ";
}
return 0;
}
从 i=0开始到 i = 数组长度。
5. newarray[i]=oldarray[i](只适用于 C + +)
C + + 程序
#include<iostream>
using namespace std;
int main()
{
int initA[100],finA[100],i,size;
cout<<"Input the size of the array : ";
cin>>size;
cout<<"Input the elements of the first array";
for(i=0;i<size;i++)
{
cin>>initA[i];
}
for(i=0;i<size;i++)
{
finA[i]=initA[i];
}
cout<<"The final array is\n";
for(i=0;i<size;i++)
cout<<finA[i]<<" ";
return 0;
}