1- every variable declared outside of functions are static and have the default value of zero(0) .
int dp[100];
bool vis[100];
struct point { int x,y; };
point ps[100];
int main(){
// all elements of dp are zero by default
// all elements of vis are false
// all points have x == 0 and y == 0
}
2- ,
is an operator and returns its rightmost statement value , for example if A
and B
be statements (A,B) == B
.
if,for,while
without a block { }
take one statement so you can simply write multiple statements separated bye ,
int main() {
int a = 1, b = 2;
int c = a,b; // c == 2
int A[100];
int sumA = 0;
for(int i = 0; i < 100; ++i)
cin >> A[i],
sumA += A[i];
}
3- if you declare an array inside a function the value of its elements are garbage , but if you write TYPE var[LEN] = { };
all elements will be zero by default.
int main(){
int A[100] ; // elements of A have garbage values
int B[100] = { } ; // all elements of B are zero
}
4- memset
is defined in <string.h>
and sets the bye values of a block of memory . you can use memset
to initialize all elements of an integer array to 0
or -1
but not the other values .
int main(){
int A[100];
memset(A,0,sizeof(A)); // all elements of A are zero
memset(A,-1,sizeof(A)); // all elements of A are -1
//NOT WORKING
memset(A,other_numbers,sizeof(A)); // WRONG
}
help me update this post with other useful facts . have fun !
Actually, you can
memset
correctly with 256 different values, for example,0x3F3F3F3F
.humm , right (tnx) .
So what's the point to use such complex constants where you can make mistake — isn't it easier to just iterate and assign all values?
totally agree ! :)
This particular constant can be used as an infinity in some contest solutions.
actually it think you mean
0x7F
.No, I don't. Often I'd like the following to not overflow:
It's summing two values which can be infinities.
Having to add up three or more values which can all be infinities is much less frequent.
A[i] != INT_MAX, btw
you are right .
A[i] = 2139062143
andINT_MAX = 2147483647
can i initialize to
INT_MAX
?no way with
memset
. you may use std::fillI think it's even easier to use std::vector and its constructor, for most cases.
I didn't know about this one:
I tried in java but doesn't work.
I don't know about
Java
, its all aboutC/C++
.I use
fill()
to initialize elements to any values. It's defined in<algorithm>
.About memset: 0 and -1 work normal because memset fills bytes with a value, and 0 is "00…000", (-1) is "11…111" in binary format, so all bytes in (usually) int are filled like in a single char. So, if you use one-byte types (like [unsigned] char) you can use memset for any value (once I used it trying to avoid TL as std::fill works a little slower).
About
int B[100] = {};
You can also initialize like this arrays and STL containers to the custom sequence of values. This is true for C++11.this works for arrays even without
C++11
:Arrays not initialized to zeroes, memset, various other stuff... what about vector<>s? You can resize them at initialization...
*insert trollface here*
Yes, you can.
vector<>
s are initialized with default values (which is zero for embedded types), but it's done with something likefill
, somemset
is definetely not slower (I think it should be even faster, because it can set memory by very big blocks using CPU vector operations instead of just int-by-int filling).I see. Still, it doesn't really hurt in a programming contest, unless the time limits are really tight.
The 1st, 2nd, 4th tips are very basic. But this is the first time I see the third tip.
fill
form includestring.h
is more faster thanmemset
and you can fill any value you want into arrays , vectors , etc . Examples :No its not !
Edit: the data is not relevant, see the comment below.
It depends on the optimization level. With MinGW GCC 4.8.1:
(
optimization flag: memset / fill
)The usual choice for programming contests is
-O2
.310/63
for/Ox
in VS 2013 . No morememset
, i'm gonna usefill
.Ouch, sorry! The above's all wrong. The reason is that the whole benchmark is flawed. The first method actually accounts for allocations, too. Swap them, and you get the opposite picture.
Here's a more relevant one. With
-O2
,memset
is 2x faster. With-O3
, there's hardly a difference."The first method actually accounts for allocations, too. Swap them, and you get the opposite picture." ???
can you explain a little bit ! (allocation ? Swap ??)
In your program above, swap the lines with
fill
andmemset
. The timings will be different, at least they are when I do it locally.I don't think this is a good test case, because the compiler can optimize out calls to
memset
/fill
.No, the strange results are because the benchmark was flawed (see above).
std::fill
is from<algorithm>
memset()
in most cases, equal in some cases (char
/wchar_t
), but almost never faster thanmemset()
.Maybe he meant that "fill" is 2 characters shorter then "memcpy"?