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

后端 未结 8 620
慢半拍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条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-01-31 07:47

    Besides what other answers mention, I would recommend you to use std::vector::assign rather than std::vector::resize and memcpy:

    void MyClass::OnImageReceived(unsigned char *pPixels, unsigned int uPixelCount)
    {
        m_pImageBuffer.assign(pPixels, pPixels + uPixelCount);
    }
    

    That will resize if necessary, and you would be avoiding the unnecessary 0 initialization of the buffer caused by std::vector::resize.

提交回复
热议问题