Memset on vector C++

后端 未结 4 1274
半阙折子戏
半阙折子戏 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:49

    You can use assign method in vector:

    Assigns new contents to the vector, replacing its current contents, and modifying its size accordingly(if you don't change vector size just pass vec.size() ).

    For example:

    vector vec(10, 0);
    for(auto item:vec)cout< 1
    
    for(auto item:vec)cout<

    Cited: http://www.cplusplus.com/reference/vector/vector/assign/

提交回复
热议问题