A few question of my C++ homework

I'd call this spaghetti code to be honest. A generic function that takes a closure that indexes into a captured container with another container passed as parameter to emulate a coroutine with filtering logic in the yields... I had trouble following this, a newbie with a couple months' programming experience would probably get an aneurysm.

Also sorry, but it looks like C from the 80s. Nearly-meaningless variable names without consonants, raw arrays, unnamed members, deeply-nested index-based computation... Yuck!

Here's my attempt: http://pastebin.ca/3746240

I indulged myself a little. If I'd been writing it for a newbie, I would have made it a bit simpler. But I think it's very readable to anybody who knows C++ properly.

Notice that, if you remove the definitions of the functions from the table, there's hardly any code at all. That's something I try to go for.

It's probably slower than yours by a fair bit, but it shouldn't be hard to optimize if needed. It uses std::set for deduplication, using vectors and deduplicating manually would likely give a huge speed improvement, but I couldn't be bothered.

(I only noticed your new version now, I'm afraid I can't dig into it too, I've already spent way too much time on this.)

/r/cpp_questions Thread Parent