1

I've recently been on leetcode looking at binary tree problems, when I noticed that for every problem, the fastest solutions in c++ had this bit of code preceding the class where you're supposed to enter your solution:

static int x=[](){
std::ios::sync_with_stdio(false);
cin.tie(NULL);
return 0;
}();

From what I understand, std::ios::sync_with_stdio(false) and cin.tie(NULL) would normally mess up the I/O streams, but because leetcode doesn't use any I/O, it's ok to change the default settings in exchange for speed.

My question is what exactly is this part of the code doing?

int x = [](){/* code */}();

Is it initializing an array and then calling its newly-defined constructor? Is it some short-form that I've just never seen before? I'm really confused as to what it's doing (aside from executing the block of code in the middle). Thanks in advance!

David McNamee
  • 403
  • 4
  • 10

0 Answers0