vector vs string for binary data

后端 未结 9 2161
日久生厌
日久生厌 2020-12-05 07:25

Which is a better c++ container for holding and accessing binary data?

std::vector

or

std::string
         


        
9条回答
  •  离开以前
    2020-12-05 07:33

    As far as readability is concerned, I prefer std::vector. std::vector should be the default container in this case: the intent is clearer and as was already said by other answers, on most implementations, it is also more efficient.

    On one occasion I did prefer std::string over std::vector though. Let's look at the signatures of their move constructors in C++11:

    vector (vector&& x);

    string (string&& str) noexcept;

    On that occasion I really needed a noexcept move constructor. std::string provides it and std::vector does not.

提交回复
热议问题