How do I clear/empty a C++ array? Theres array::fill, but looks like its C++11 only? I am using VC++ 2010. How do I empty it (reset to all 0)?
-
1Resetting all to 0 is not emptying.leftaroundabout– leftaroundabout2012-11-09 12:39:43 +00:00Commented Nov 9, 2012 at 12:39
-
What do you mean clear/empty? An array is never empty.juanchopanza– juanchopanza2012-11-09 12:39:54 +00:00Commented Nov 9, 2012 at 12:39
-
Huh? The array of zero-size is mostly empty.Michael Krelin - hacker– Michael Krelin - hacker2012-11-09 12:41:47 +00:00Commented Nov 9, 2012 at 12:41
-
1@MichaelKrelin-hacker Right, I should have phrased it "an array can never be emptied"juanchopanza– juanchopanza2012-11-09 12:48:00 +00:00Commented Nov 9, 2012 at 12:48
-
1@juanchopanza More generally: the size of an array can never be changes. (Emptying is just a special case of the more general rule.) And of course, since there's no case where you'd ever dynamically allocate an array, it's probably OK to say that an array can never be empty.James Kanze– James Kanze2012-11-09 12:50:04 +00:00Commented Nov 9, 2012 at 12:50
7 Answers
std::fill_n(array, elementCount, 0);
Assuming array is a normal array (e.g. int[])
1 Comment
Assuming a C-style array a of size N, with elements of a type implicitly convertible from 0, the following sets all the elements to values constructed from 0.
std::fill(a, a+N, 0);
Note that this is not the same as "emptying" or "clearing".
Edit: Following james Kanze's suggestion, in C++11 you could use the more idiomatic alternative
std::fill( std::begin( a ), std::end( a ), 0 );
In the absence of C++11, you could roll out your own solution along these lines:
template <typename T, std::size_t N> T* end_(T(&arr)[N]) { return arr + N; }
template <typename T, std::size_t N> T* begin_(T(&arr)[N]) { return arr; }
std::fill( begin_( a ), end_( a ), 0 );
4 Comments
std::fill( std::begin( a ), std::end( a ), 0 ). Somehow this seems more idiomatic. (And is there anyone using pre-C++11 who doesn't have something similar in their toolkit?)ints!Hey i think The fastest way to handle that kind of operation is to memset() the memory.
Example-
memset(&myPage.pageArray[0][0], 0, sizeof(myPage.pageArray));
A similar C++ way would be to use std::fill
char *begin = myPage.pageArray[0][0];
char *end = begin + sizeof(myPage.pageArray);
std::fill(begin, end, 0);
3 Comments
std::memset is not faster than std::fill.If only to 0 then you can use memset:
int* a = new int[6];
memset(a, 0, 6*sizeof(int));
18 Comments
memset. Use the appropriate C++ equivalents instead of the C functions. And don’t use manually managed memory for dynamic arrays. Use std::vector instead.memset is just fine, but only provided this is a kind of array the OP meant. Another - more serious - issue is that the second parameter is count. Meaning 6*sizeof(*a).std::fill.memset is still allowed even if you dislike it. And it's a joy to see memset once in a while for an old-fashioned hacker ;-) That said, I'm glad we both expressed our opinions. This way the empact will neither be eradication memset nor abuse thereof ;)memset is too error prone, given that there are safer alternatives (which are typically faster as well). It has the additional disadvantage in what it tells the reader: that the author doesn't really know C++.Should you want to populate the array with something other than intrinsic values, std::fill wont cut it; instead I found std::generate useful. e.g. I had a vector of lists I wanted to initialize
std::generate(v.begin(), v.end(), [] () { return std::list<X>(); });
You can do ints too e.g.
std::generate(v.begin(), v.end(), [n = 0] () mutable { return n++; });
or just
std::generate(v.begin(), v.end(), [] (){ return 0; });
but I imagine std::fill is faster for the simplest case