mercredi 24 avril 2019

Filling an std::vector with raw data

I need to fill a vector with raw data, sometimes 2 bytes, sometimes 8... I ended up with this template function:

template <typename T>
void fillVector(std::vector<uint8_t>& dest, T t)
{
    auto ptr = reinterpret_cast<uint8_t*>(&t);
    dest.insert(dest.end(),ptr,ptr+sizeof(t));
}

with this I can fill the vector like this:

fillVector<uint32_t>(dst,32bitdata);
fillVector<uint16_t>(dst,16bitdata);

I was wondering if something more similar already exist in the standard library, maybe I just reinvented the wheel

Aucun commentaire:

Enregistrer un commentaire