C++使用memcpy填充结构中的向量

tmi*_*hty 0 c++ struct memcpy

我想用memcpy填充结构.

结构声明如下:

struct udtFeatures
{
    vector<unsigned char>ByteFeatures;
};
Run Code Online (Sandbox Code Playgroud)

这是我想要填充字节的地方:

void clsMapping::FeedFeaturesFromMap(udtFeatures &uFeatures,int uOtherIndex)
{
    int iBytePos=this->Content()[uOtherIndex].ByteStart;
    int iByteCount=this->Content()[uOtherIndex].ByteCount;

    memcpy(uFeatures.ByteFeatures, &((char*)(m_pVoiceData))[iBytePos],iByteCount);
}
Run Code Online (Sandbox Code Playgroud)

然而memcpy不喜欢这个.

编译器说:

找不到匹配的转换函数

std::vector<unsigned char, std::allocator<unsigned char>> in void *.
Run Code Online (Sandbox Code Playgroud)

我猜是因为它.ByteFeatures只是一个指针?

我怎么能这样做?

Mik*_*our 5

我猜是因为它.ByteFeatures只是一个指针?

不,这是因为它只是一个指针.这是一个载体.

我怎么能这样做?

如果你想要一个指向由向量管理的数组的指针,那就是uFeatures.ByteFeatures.data()&uFeatures.ByteFeatures[0].或者,您可以考虑使用std::copy.

在任何一种情况下,确保向量足够大,然后再将其复制到其中.