简体   繁体   English

如何从原始数组到std :: vector执行等效的memcpy?

[英]How can I do equivalent of memcpy from a raw array to a std::vector?

I have a class that has (amongst many other things) a pointer to unsigned char that gets deleted and reallocated to store some data from another array. 我有一个类(在许多其他事物中)有一个指向unsigned char的指针,它被删除并重新分配以存储来自另一个数组的一些数据。 This done with a function 这是通过一个功能完成的

 class MyClass {
  private:
      unsigned char* m_Buffer;
      int m_BufferSize;
  public:
      bool SetBuffer(int iSize, const unsigned char* pArray); 
 };

 bool MyClass::SetBuffer(int iSize, const unsigned char* pArray) {
     bool bOK = false;
     if (pArray != NULL  && iSize > 0) {
         delete [] m_Buffer;
         m_Buffer = new unsigned char[iSize];
         memcpy(m_Buffer,pArray,iSize);
         m_BufferSize = iSize;
         bOK = true;
      }
      return bOK;
  }

I dont like this code at all, and I would really like to replace the pointer with a std::vector<unsigned char> . 我根本不喜欢这个代码,我真的想用std::vector<unsigned char>替换指针。 My question is, how would I perform the memcpy aspect? 我的问题是,我将如何执行memcpy方面? If I were passing a vector in as argument to my function, I could copy it using iterators, but I have no control over parameter argument type so I am stuck with unsigned char* . 如果我将一个向量作为参数传递给我的函数,我可以使用迭代器复制它,但我无法控制参数参数类型,所以我坚持使用unsigned char* Is there a way of using iterators, or sizing the vector to the right size and then accessing its internal array so that I can still copy the data with memcpy ? 有没有办法使用迭代器,或者将矢量调整到合适的大小,然后访问其内部数组,以便我仍然可以使用memcpy复制数据? Or even better something using iterators?? 甚至更好的东西使用迭代器? I know I could use a loop and push_back but that seems painfully inefficient to me. 我知道我可以使用循环和push_back但这对我来说似乎非常低效。 Any suggestions will be gratefully received. 我们将非常感激地收到任何建议。

Actually, iterators are modelled from pointers and therefore pointers within an array are considered to implement the RandomAccessIterator concept. 实际上,迭代器是从指针建模的,因此数组中的指针被认为是实现RandomAccessIterator概念。

Therefore: 因此:

m_buffer.assign(pArray, pArray + Size);

I really advise you to avoid raw pointers this way. 我真的建议你这样避免原始指针。 I think that is a better idea to manage std::vectors instead of the raw pointers. 我认为管理std :: vectors而不是原始指针是一个更好的主意。

class MyClass {
private:
    std::vector<unsigned char> m_Buffer;
    // No size member is needed, it is stored in m_Buffer
public:
    // No size parameter is needed, it is stored in Array
    void SetBuffer(const std::vector<unsigned char> &Array);
};

void MyClass::SetBuffer(const std::vector<unsigned char> &Array) {
    std::copy(Array.begin(), Array.end(), m_Buffer.begin());
}

Assuming that your dessing forces to have a raw managed for MyClass you will take care of this pointer on copy constructors and operator = (or get rid of it instead): 假设你的dessing force有一个为MyClass管理的原始,你将在复制构造函数和operator =上处理这个指针(或者取而代之):

MyClass::MyClass(const MyClass &Class) {
    m_BufferSize = Class.m_BufferSize;
    m_Buffer = new new unsigned char[m_BufferSize];
    memcpy(m_Buffer, Class.m_Buffer, m_BufferSize); 
}

MyClass::operator =(const MyClass &Class) {
    if (m_Buffer) delete [] m_Buffer;
    m_BufferSize = Class.m_BufferSize;
    m_Buffer = new new unsigned char[m_BufferSize];
    memcpy(m_Buffer, Class.m_Buffer, m_BufferSize); 
}

If you don't take care of the MyClass managed pointers in the copy constructor and operator = you will end with two instances of MyClass managing the same memory. 如果你不在复制构造函数和operator =中处理MyClass托管指针,你将以两个管理相同内存的MyClass实例结束。

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

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