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
2009-11-03 03:17:25
+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
2009-11-03 03:17:41
What is POD type?
avd
2009-11-03 03:19:03
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
2009-11-03 03:19:23
"Plain old data" - integers, structs (that contain only PODs), chars...
LiraNuna
2009-11-03 03:20:13
Really? I thought it was common knowledge that a `vector`'s memory had to be contiguous. Is that just a rumor?
GMan
2009-11-03 03:20:17
@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
2009-11-03 03:24:38
@GMan: IIRC, the std::vector<bool> specialization is a counterexample, and probably the most well-known one.
bcat
2009-11-03 03:38:16
GMan
2009-11-03 03:40:35
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
2009-11-03 03:40:55
Well, yeah, `std::vector<bool>` has always been a pain in the-- a super duper helpful specialization.
GMan
2009-11-03 03:41:26
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
2009-11-03 03:45:53
@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
2009-11-03 03:48:31
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
2009-11-03 03:57:23
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
2009-11-03 04:00:50
@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
2009-11-03 04:18:28
However, this won't work with vector<bool> !
Dave
2009-11-03 06:30:18
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
2009-11-03 05:28:39