Converting std::string to std::vector

Nope, that’s the way to do it, directly initializing the vector with the data from the string.

As @ildjarn points out in his comment, if for whatever reason your data buffer needs to be null-terminated, you need to explicitly add it with charvect.push_back('\0').

Also note, if you want to reuse the buffer, use the assign member function which takes iterators.

Leave a Comment