有一个函数复制C / C + +中的数组?

我是学习C / C ++的Java程序员。 所以我知道Java有一个像System.arraycopy()这样的函数。 复制一个数组。 我想知道是否有在C或C + +复制一个数组的function。 我只能通过使用for循环,指针等find实现复制数组。 有没有我可以用来复制数组的函数?

由于C ++ 11,你可以直接复制数组与std::array

 std::array<int,4> A = {10,20,30,40}; std::array<int,4> B = A; //copy array A into array B 

这里是关于std :: array的文档

既然你问了一个C ++解决scheme…

 const int arr_size = 10; some_type src[arr_size]; // ... some_type dest[arr_size]; std::copy(std::begin(src), std::end(src), std::begin(dest)); 

正如其他人所说,在C中,你会使用memcpy 。 但是请注意,这是一个原始的内存拷贝,所以如果你的数据结构有指向它们自己或者指向对方的指针,拷贝中的指针仍然会指向原来的对象。

在C ++中,如果你的数组成员是POD(也就是说,本质上也可以在C中使用的types),也可以使用memcpy ,但是一般情况下, memcpy将不被允许。 正如其他人提到的,使用的函数是std::copy

话虽如此,在C ++中,你很less应该使用原始数组。 相反,你应该使用其中一个标准的容器( std::vector是最接近内置数组,也是我认为离Java数组最近的 – 比普通的C ++数组更接近 – 的确,但std::deque或者std::list在某些情况下可能更合适),或者,如果使用C ++ 11, std::array非常接近内置数组,但是具有像其他C ++types的值语义。 我在这里提到的所有types都可以通过分配或复制来复制。 此外,可以使用迭代器语法从opne“交叉复制”到另一个(甚至是内置数组)。

这给出了可能性的概述(我认为所有相关的头文件已包括在内):

 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; } 

你可以使用memcpy()

 void * memcpy ( void * destination, const void * source, size_t num ); 

memcpy()将来自source指向的位置的num个字节的值直接复制到destination指向的内存块。

如果destinationsource重叠,则可以使用memmove()

 void * memmove ( void * destination, const void * source, size_t num ); 

memmove()将来自source指向的位置的num个字节的值复制到destination指向的内存块。 复制就像使用中间缓冲区一样,允许目标和源重叠。

在C中使用memcpy ,在C ++中使用std::copy

在C中,你可以使用memcpy 。 在C ++中,使用<algorithm>标题中的std::copy

在C ++ 11中,您可以使用适用于std容器的Copy()

 template <typename Container1, typename Container2> auto Copy(Container1& c1, Container2& c2) -> decltype(c2.begin()) { auto it1 = std::begin(c1); auto it2 = std::begin(c2); while (it1 != std::end(c1)) { *it2++ = *it1++; } return it2; } 

首先,因为您正在切换到C ++,build议使用向量来代替传统数组 。 此外,要复制一个数组或向量, std::copy是您的最佳select。

访问此页面以获取如何使用复制function: http : //en.cppreference.com/w/cpp/algorithm/copy

例:

 std::vector<int> source_vector; source_vector.push_back(1); source_vector.push_back(2); source_vector.push_back(3); std::vector<int> dest_vector(source_vector.size()); std::copy(source_vector.begin(), source_vector.end(), dest_vector.begin()); 

我在这里给出了两种应对数组的方法,用于C和C ++语言。 memcpy和复制两个可用于C + +但复制是不可用的C,你必须使用memcpy,如果你试图复制C中的数组

 #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; }