我知道Java有一个函数System.arraycopy();
可以复制一个数组。我想知道C或C++中是否有一个函数可以复制一个数组。我只能找到使用for
循环、指针等来实现复制数组的方法。但是是否有一个库函数可以用来复制数组呢?
既然你要求用C++解决...
#include <algorithm>
#include <iterator>
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));
<iterator>
中。 - Ed S.<algorithm>
中有复制吗? - Jerry Jeremiahstd::array
直接复制数组:#include <array>
std::array<int, 4> A = {10, 20, 30, 40};
std::array<int, 4> B = A; // Copy array A into array B
B = A
。 - swalog正如其他人所提到的,在C语言中,您可以使用memcpy
。但请注意,这将执行原始内存复制操作,因此如果您的数据结构具有指向自身或彼此的指针,则副本中的指针仍将指向原始对象。
在C++中,如果您的数组成员是POD(即实质上是您也可以在C中不加修改地使用的类型),则同样可以使用memcpy
,但通常情况下,将不允许使用memcpy
。就像其他人提到的一样,要使用的函数是std::copy
。
话虽如此,在C++中你很少需要使用原始数组。相反,你应该使用标准容器之一(std::vector
是最接近内置数组的容器,也比纯C++数组更接近Java数组 - 实际上更接近 - 但在某些情况下,std::deque
或std::list
可能更合适),或者如果你使用C++11,可以使用std::array
,它非常接近内置数组,但像其他C++类型一样具有值语义。我在这里提到的所有类型都可以通过赋值或复制构造进行复制。此外,你可以使用迭代器语法从一个容器复制到另一个容器(甚至从内置数组中复制)。
这提供了可能性的概述(我假设所有相关的头文件都已经被包含):
#include <vector>
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
示例中,那不是唯一的错误;大小也是错误的。我现在已经修复了它,谢谢。 - celtschk在 C 中使用 memcpy
,在 C++ 中使用 std::copy
。
memcpy
应该是编译器内置的)? - wcochran您可以使用memcpy()
函数,
void * memcpy ( void * destination, const void * source, size_t num );
memcpy()
函数将从指定的源内存地址中复制给定长度(num
)的数据到目标内存地址。
如果目标内存地址和源内存地址有重叠,则应该使用memmove()
函数。
void * memmove ( void * destination, const void * source, size_t num );
memmove()
函数将从source
指向的位置复制num
个字节的值到指向内存块的destination
指针所指向的位置。复制操作就像使用中间缓冲区一样进行,从而允许目标和源重叠。
memcpy
不适用的情况,即复制字节是不够的。 - Ed S.#include <algorithm>
...
const int bufferSize = 10;
char* origArray, newArray;
std::copy(origArray, origArray + bufferSize, newArray);
memcpy
。在C++中,请使用来自<algorithm>
头文件的std::copy
。我在这里提供了两种处理数组的方法,适用于C和C++语言。 在C ++中,可以使用memcpy和copy,但copy不能用于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;
}
using namespace std;
是一个不好的实践,不要使用它。请使用包含更好、更更新的标准文档的cppreference.com而不是cplusplus.com。在C++中,stdio.h
的等效头文件是cstdio
,请使用后者。此外,该代码相当不符合C++的惯用法。如果您展示C和C++的单独解决方案,那么代码将更加清晰易懂。 - tambre只需在您的代码中包含标准库即可。
#include<algorithm>
数组大小将用n
表示。
您的旧数组:
int oldArray[n]={10,20,30,40,50};
声明一个新的数组,将旧数组的值复制到其中
int newArray[n];
copy_n(oldArray,n,newArray);
在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;
}
std::end
从循环中提取出来(因为 c1 在循环期间不会更改或使迭代器失效,所以重新计算结束位置是不必要的)。 - celtschk
man memmove
和man memcpy
这两个命令是C语言中用来移动内存块的函数。它们都可以将从源地址开始的一段内存复制到目标地址,但它们的行为略有不同。memcpy()
函数将源地址中指定的字节数复制到目标地址,如果目标地址和源地址重叠,则结果不确定。memmove()
函数也将源地址中指定的字节数复制到目标地址,但它可以处理目标地址和源地址重叠的情况。在这种情况下,memmove()
函数会以一种安全而可预测的方式执行复制操作。因此,如果您需要移动一个内存块,并且知道目标地址与源地址不会重叠,那么使用memcpy()
函数可能更高效。但如果您不能确定目标地址是否与源地址重叠,或者您确信它们重叠,那么使用memmove()
函数更加安全。 - user142162memcpy
,改用std::copy
。如果您的类型有一个有意义的复制构造函数,则memcpy
会出错。 - Ed S.