Блог пользователя unt311

Автор unt311, история, 3 года назад, По-английски

Please share your dfs with lambda function code or improve mine.

        function<void()> dfs = [&](int a, int par, int depth) {
            vis[a] = true;
            if(depth > maxDepth){
                maxDepth = depth;
                farthestNode = a;
            }
            for(auto x: adj[a]){
                if(!vis[x])
                    dfs(x, a, 1 + dep);
            }
        };

I get the following error. error: no match for call to '(std::function<void()>) (long long int&, long long int&, long long int)'|

NOTE: changing a to &a and p to &p in parameter list does not make the error go away.

  • Проголосовать: нравится
  • +8
  • Проголосовать: не нравится

»
3 года назад, # |
  Проголосовать: нравится +5 Проголосовать: не нравится

I have #define int long long in the beginning of my code, in case you wonder where did long long come from.

»
3 года назад, # |
  Проголосовать: нравится +17 Проголосовать: не нравится

Try function <void(int, int, int)> instead.

»
3 года назад, # |
  Проголосовать: нравится +47 Проголосовать: не нравится

I like to write them like this:

vi col(n);
col[0] = 0;

auto dfs = [&](int u, int p, auto&& dfs) -> void {
	for (int v : adj[u])
		if (v != p) {
			col[v] = col[u] ^ 1;
			dfs(v, u, dfs);
		}
};

dfs(0, -1, dfs);
»
3 года назад, # |
  Проголосовать: нравится 0 Проголосовать: не нравится

Can anyone have the link on how to write recursive lambda and related stuff?

  • »
    »
    3 года назад, # ^ |
      Проголосовать: нравится +1 Проголосовать: не нравится

    What do you mean? How lightseba's answer not tutorial for recursive lambda?

    • »
      »
      »
      3 года назад, # ^ |
        Проголосовать: нравится 0 Проголосовать: не нравится

      i want to know more about lambda function(especially recursive) like why '&&' operater(not using && or single '&' also working) in ([&](int u, int p, auto&& dfs)) is used and many more...

      and yes lightseba's answer helped me to write recursive lambda function but i want know more that's why i asked for resources if anyone has..

      • »
        »
        »
        »
        3 года назад, # ^ |
          Проголосовать: нравится +5 Проголосовать: не нравится

        If you ask about basic knowledge, then I think you can start here and there

        Recursive lambda as lightseba's corresponds to C++14's generic lambda
        It's about labmda itself. About auto&& there no answer as I see. But you can see from paper that it is equivalent to

        template<typename Func>
        void operator()(int u, int p, Func&& dfs) {
            //...
        }
        

        And that syntax is about universal references. Can read some information here. Don't know whether there fuller paper about it but it is hard to understand topic.
        In fact in recursive calls passes lvalue reference to lambda. auto& must work too, difference there if std::move or rvalue used to pass lambda (may not compile). auto must work too, I'd say that difference here is that lambda is copying, but I can't reproduce it.