Is it good practice to use std::vector as a simple buffer?

后端 未结 8 690
慢半拍i
慢半拍i 2021-01-31 07:41

I have an application that is performing some processing on some images.

Given that I know the width/height/format etc. (I do), and thinking just about defining a buffer

8条回答
  •  忘了有多久
    2021-01-31 07:52

    In addition - to ensure a minimum of allocated memory:

    void MyClass::OnImageReceived(unsigned char *pPixels, unsigned int uPixelCount)
    {
        m_pImageBuffer.swap(std::vector(
             pPixels, pPixels + uPixelCount));
        // ... process image etc. ...
    }
    

    vector::assign does not change the amount of memory allocated, if the capacity is bigger than the amount needed:

    Effects: erase(begin(), end()); insert(begin(), first, last);

提交回复
热议问题