Fastest way to reset every value of std::vector to 0

前端 未结 6 2046
执念已碎
执念已碎 2020-12-07 07:31

What\'s the fastest way to reset every value of a std::vector to 0 and keeping the vectors initial size ?

A for loop with the [] operator ?

6条回答
  •  轮回少年
    2020-12-07 07:55

    If it's just a vector of integers, I'd first try:

    memset(&my_vector[0], 0, my_vector.size() * sizeof my_vector[0]);
    

    It's not very C++, so I'm sure someone will provide the proper way of doing this. :)

提交回复
热议问题