fastest way to convert a std::vector to another std::vector

后端 未结 8 2414
春和景丽
春和景丽 2020-12-03 14:08

What is the fastest way (if there is any other) to convert a std::vector from one datatype to another (with the idea to save space)? For example:

std::vector         


        
8条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-03 14:34

    The fastest way to do it is to not do it. For example, if you know in advance that your items only need a byte for storage, use a byte-size vector to begin with. You'll find it difficult to find a faster way than that :-)

    If that's not possible, then just absorb the cost of the conversion. Even if it's a little slow (and that's by no means certain, see Nicol's excellent answer for details), it's still necessary. If it wasn't, you would just leave it in the larger-type vector.

提交回复
热议问题