简体   繁体   English

C ++等同于Java的System.arraycopy

[英]C++ equivalent to Java's System.arraycopy

I'm trying to port some Java code of mine that makes heavy use of the System.arraycopy method and want to know if there is an equivalent in C++. 我正在尝试移植一些我的Java代码,这些代码大量使用System.arraycopy方法,并且想知道C ++中是否有等效代码。 Basically I want to have n byte arrays and combine them into one big array. 基本上我想拥有n个字节的数组并将它们组合成一个大数组。 Each of the initial arrays can be of variable length, so I don't want to go through the hoops of calculating the end arrays length and then populating the entire array one position at a time as this feels rather slow and I'm sure this operation has been optimized. 每个初始数组的长度都可以变化,所以我不想花时间计算最终数组的长度,然后一次将整个数组填充一个位置,因为这感觉很慢,我敢肯定操作已优化。 However, I can't find what this optimization is (although I may be making this more complicated than it should be). 但是,我找不到这个优化是什么(尽管我可能使它变得比它应该的更复杂)。

Here's some pseudo (Java) code to illustrate what I want to do. 这是一些伪(Java)代码,用以说明我要执行的操作。

byte[] a = new byte[]{0x00, 0x01, 0x02};
byte[] b = new byte[][0x03, 0x04, 0x05];
byte[] ab = new byte[a.length+b.length];
System.arraycopy(ab, 0, a, 0, a.length);
System.arraycopy(ab, a.length+1, b, 0, b.length);
//Now, I would expect ab to look like {0x00, 0x01, 0x02, 0x03, 0x04, 0x05}

Like I said, this may be simple in C++, but I will be doing this many, many times and want to make sure I'm doing it as efficiently as possible. 就像我说的那样,这在C ++中可能很简单,但是我会做很多很多次,并希望确保自己尽可能地高效。

Given a_len and b_len (containing the length in bytes of a and b), and a dst buffer big enough to hold both arrays, you can use memcpy. 给定a_len和b_len(包含a和b的字节长度),以及一个足以容纳两个数组的dst缓冲区,您可以使用memcpy。 Note: this also depends on dst being declared as a pointer to byte size data. 注意:这还取决于将dst声明为指向字节大小数据的指针。

memcpy( dst, a, a_len );
memcpy( dst+a_len, b, b_len );

This works well for primitive types (as it looks like you're copying byte arrays around)... If you need to copy objects, take a look at std::copy<>(). 这对于原始类型非常有效(看起来好像您正在复制字节数组)...如果需要复制对象,请查看std :: copy <>()。

Try this: 尝试这个:

#include <vector>

int main()
{
    typedef unsigned char Byte;
    std::vector<Byte> a;
    std::vector<Byte> b;
    // Fill vectors a and b

    std::vector<Byte> ab;
    // Reserve enough memory to fit a and b in order to avoid
    // unnecessary reallocations.
    ab.reserve(a.size() + b.size());
    ab.insert(ab.end(), a.begin(), a.end());
    ab.insert(ab.end(), b.begin(), b.end());

    return 0;
}

In C++, std::vector is your friendly neighborhood dynamically re-sizable array. 在C ++中, std::vector是您的友好邻域可动态调整大小的数组。 It is just as fast as regular arrays for random access. 与用于随机访问的常规数组一样快。 It is well worth the time to study std::vector and other containers/algorithms in the standard library. 在标准库中研究std::vector和其他容器/算法非常值得。 I recommend the C++ standard library book by Josuttis. 我推荐Josuttis的C ++标准库书。

vector::insert on vectors of basic types will probably be just as fast as doing C-style memcpy on C arrays. 在基本类型的向量上vector::insert可能和在C数组上执行C风格的memcpy一样快。 I'd be very surprised if it weren't. 如果不是的话,我会感到非常惊讶。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM