50

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

(Not clear() or erase() method, I want to retain the size of vector, I just want to initialize all the values.)

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
avd
  • 13,993
  • 32
  • 78
  • 99

5 Answers5

99

Use std::fill():

std::fill(myVector.begin(), myVector.end(), 0);
Adam Rosenfield
  • 390,455
  • 97
  • 512
  • 589
  • 11
    If you're worried about efficiency, most STL implementations will have specialized std::fill implementations that do memsets where possible. – razeh Apr 23 '13 at 19:51
  • 1
    @razeh Currently, `-O3` is required for gcc to optimize fill to be as fast as memset. Clang only needs `-O2`. (https://stackoverflow.com/a/51274937/5267751) – user202729 Oct 27 '19 at 02:36
30

If your vector contains POD types, it is safe to use memset on it - the storage of a vector is guaranteed to be contiguous.

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

Edit: Sorry to throw an undefined term at you - POD stands for Plain Old Data, i.e. the types that were available in C and the structures built from them.

Edit again: As pointed out in the comments, even though bool is a simple data type, vector<bool> is an interesting exception and will fail miserably if you try to use memset on it. Adam Rosenfield's answer still works perfectly in that case.

Community
  • 1
  • 1
Mark Ransom
  • 299,747
  • 42
  • 398
  • 622
  • Technically, a vector's storage isn't 100% guaranteed to be contiguous by the C++03 standard, but all implementations implement it that way, so memset is indeed safe with POD types. C++0x fixes the problem and requires all vector implementations to use contiguous storage. – Adam Rosenfield Nov 03 '09 at 03:19
  • 1
    Really? I thought it was common knowledge that a `vector`'s memory had to be contiguous. Is that just a rumor? – GManNickG Nov 03 '09 at 03:20
  • 1
    @GMan: Yep. Take a look at section 23.2.4 of the C++03 draft standard at ftp://ftp.research.att.com/dist/c++std/WP/CD2/body.pdf . Nowhere does it mention contiguous storage. – Adam Rosenfield Nov 03 '09 at 03:24
  • @GMan: IIRC, the std::vector specialization is a counterexample, and probably the most well-known one. – bcat Nov 03 '09 at 03:38
  • 6
    In my copy of the ISO03 standard, on page 489 [23.2.4], it says: "The elements of a `vector` are stored contiguously, meaning that if v is a `vector` where T is some type other than `bool`, then it obeys the identity `&v[n] == &v[0] + n` for all `0 <= n < v.size()`." I think that means contiguous, right? – GManNickG Nov 03 '09 at 03:40
  • 1
    I was under the impression that contiguous storage was guaranteed by the current standards - see http://stackoverflow.com/questions/247738/is-it-safe-to-assume-that-stl-vector-storage-is-always-contiguous – Mark Ransom Nov 03 '09 at 03:40
  • Well, yeah, `std::vector` has always been a pain in the-- a super duper helpful specialization. – GManNickG Nov 03 '09 at 03:41
  • But to fair, it really doesn't say it in the draft, so if the draft is your personal standard that could be misleading. Comment +1 just for noticing. :) – GManNickG Nov 03 '09 at 03:45
  • 3
    @Adam Rosenfield: you may be thinking of strings being contiguous or not. As for vectors, "contiguity is in fact part of the vector abstraction. It’s so important, in fact, that when it was discovered that the C++98 standard didn’t completely guarantee contiguity, the C++03 standard was amended to explicitly add the guarantee" from http://herbsutter.wordpress.com/2008/04/07/cringe-not-vectors-are-guaranteed-to-be-contiguous/ – Michael Burr Nov 03 '09 at 03:48
  • And as far as `vector` being an exception, `vector` has many other problems and anomalies: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/1999/n1185.pdf and various other papers on problems with `vector` – Michael Burr Nov 03 '09 at 03:57
  • You're right, I'm mistaken. I think it was the C++98 standard that didn't guarantee contiguous storage and the C++03 standard fixed that. But, as I only have the draft standard and not the real standard, I can't be certain. – Adam Rosenfield Nov 03 '09 at 04:00
  • @Adam: C++98 did not require that vector is contiguous explicitly. Interestingly enough the TC was accepted before the standard was published - re: http://www.comeaucomputing.com/iso/lwg-defects.html#69 – D.Shawley Nov 03 '09 at 04:18
4

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<int> vec(10, 0);
for(auto item:vec)cout<<item<<" ";
cout<<endl;
// 0 0 0 0 0 0 0 0 0 0 

// memset all the value in vec to 1,  vec.size() so don't change vec size
vec.assign(vec.size(), 1); // set every value -> 1

for(auto item:vec)cout<<item<<" ";
cout<<endl;
// 1 1 1 1 1 1 1 1 1 1

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

Jayhello
  • 5,931
  • 3
  • 49
  • 56
2

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

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

Its only drawback is that it makes a copy.

Kevin Panko
  • 8,356
  • 19
  • 50
  • 61
Tim Finer
  • 29
  • 1
1

m= Number of Rows && n== Number of Columns

vector<vector<int>> a(m,vector<int>(n,0));        
sakigo
  • 95
  • 8