Boring_Day's blog

By Boring_Day, history, 2 months ago, In English

So today i was learning about c++ lambda functions. and i have some doubts.

Q.1) See this

why the output is 2?

Q.2) another question

This is giving me runtime error why?

tell me

 
 
 
 
  • Vote: I like it
  • -23
  • Vote: I do not like it

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Boring_Day (previous revision, new revision, compare).

»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

why it's 2 i have passed 'a' with refrence

[&](int a, int b)

a is passed by value here, not by reference. & is for captured variables, not for ones passed as arguments.

how can i use the get function?

sum and get are STL functions, don't name your variables like this. Anyway, in this case you should split declaration and implementation, just like with usual functions:

function<int(int)> sum2, get2;

sum2 = [&](int a) {
    cout << get2(2);
    return 0;
};

get2 = [&](int v) {
    int b = sum2(3);
    return v;
};

Also, try your best to avoid using std::function, this thing will hurt your program's performace and can easily lead to TLE. Use auto wherever possible

auto sum2 = [&](int a, int b) { ...
  • »
    »
    2 months ago, # ^ |
    Rev. 2   Vote: I like it 0 Vote: I do not like it

    How to pass 'a' as reference. And i searched online about capture clause but i couldn't understand can you explain with the help of code.

    • »
      »
      »
      2 months ago, # ^ |
        Vote: I like it 0 Vote: I do not like it

      Captured variables are variables just outside the lambda function that can be used in its body (not its arguments). They can be passed by reference [&] or by value [=].

      To pass 'a' by reference, just use 'int& a' in the lambda's header.

      • »
        »
        »
        »
        2 months ago, # ^ |
          Vote: I like it 0 Vote: I do not like it

        This is giving me error can you please explain why?

        Spoiler
        • »
          »
          »
          »
          »
          2 months ago, # ^ |
            Vote: I like it 0 Vote: I do not like it

          In C++, you have to declare & implement the function before calling it.

          This should work:

          function<int(int)> get;
          
          get = [&](int a)
          {
              return a;
          };
          
          cout << get(3);
          
          • »
            »
            »
            »
            »
            »
            2 months ago, # ^ |
              Vote: I like it 0 Vote: I do not like it

            please try to understand i want to use two functions inside each other in some dp problems. how can i do this.

            • »
              »
              »
              »
              »
              »
              »
              2 months ago, # ^ |
                Vote: I like it 0 Vote: I do not like it

              All you need is forward declaration then, like CountZero said:

              Code
              • »
                »
                »
                »
                »
                »
                »
                »
                2 months ago, # ^ |
                  Vote: I like it 0 Vote: I do not like it

                but it's not working properly see.

                this is giving me runtime error

                Spoiler
»
2 months ago, # |
  Vote: I like it 0 Vote: I do not like it

Auto comment: topic has been updated by Boring_Day (previous revision, new revision, compare).