tags:

views:

449

answers:

3

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)

+19  A: 

Use std::fill():

std::fill(myVector.begin(), myVector.end(), 0);
Adam Rosenfield
+7  A: 

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. See this Wikipedia link: http://en.wikipedia.org/wiki/Plain%5Fold%5Fdata%5Fstructures

Mark Ransom
What is POD type?
avd
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
"Plain old data" - integers, structs (that contain only PODs), chars...
LiraNuna
Really? I thought it was common knowledge that a `vector`'s memory had to be contiguous. Is that just a rumor?
GMan
@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
@GMan: IIRC, the std::vector<bool> specialization is a counterexample, and probably the most well-known one.
bcat
GMan
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
Well, yeah, `std::vector<bool>` has always been a pain in the-- a super duper helpful specialization.
GMan
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. :)
GMan
@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
And as far as `vector<bool>` being an exception, `vector<bool>` 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<bool>`
Michael Burr
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
@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
However, this won't work with vector<bool> !
Dave
A: 

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

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

It's only drawback is that it makes a copy.

Tim Finer