Recursive generator in C++

前端 未结 5 1254
星月不相逢
星月不相逢 2021-02-06 11:13

I have a vector of size = N where each element i can have values from 0 to possible_values[i]-1. I want to do a function that iterates me through all those values.

I was

5条回答
  •  迷失自我
    2021-02-06 11:22

    Generators in C++ aren't trivial but still possible with a bit of black magic:

    http://www.codeproject.com/Articles/29524/Generators-in-C

    You can look at answers to Safe cross platform coroutines because attempts to actually emulate python "yield" (including PEP 342) will bring you to some coroutine implementation anyway.

    If you want to solve your problem in C++ way, it's more common to use an object for storing state of your "non-generator" method.

提交回复
热议问题