AKASH ROY
AKASH ROY

Reputation: 13

Not able to understand the meaning of this code

I dont understand what the following code is doing

auto sgn = [&](int x) 
{
        if (x > 0) return 1;
        else return -1;
};

What is the role of ampersand here? Is it a pointer? And is this block a structure or something like that?

I came across this block of code in codeforces round 636 division 3 editorial.

Upvotes: 0

Views: 95

Answers (1)

Jesper Juhl
Jesper Juhl

Reputation: 31465

The & here means that the lambda captures all variables by reference.

The lambda returns 1 or -1 depending on the value of the argument x, so the capture really doesn't actually matter here.

Upvotes: 5

Related Questions