Memset on vector C++

后端 未结 4 1272
半阙折子戏
半阙折子戏 2020-12-13 04:22

Is there any equivalent function of memset for vectors in C++ ?

(Not clear() or erase() method, I want to retain the size of v

4条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-13 04:56

    Another way, I think I saw it first in Meyers book:

    // Swaps with a temporary.
    vec.swap( std::vector(vec.size(), 0) );
    

    Its only drawback is that it makes a copy.

提交回复
热议问题