by compusa · 13y ago · 0 comments
This is what std::reference_wrapper is for.
> I don't care what kind of data structure this came from
Fortunately, neither does the STL.
for_each(begin(foo_container), end(foo_container), some_foo_op);
Do you know what kind of container foo_container is? It doesn't matter. It can be a set, vector, list, or even a plain array.
> And then you can dream up things like the ability to take an iterator< foo > and a method on foo that will return a bar, to get an iterator< bar >
You can do this with std::transform.
transform(begin(foo_container), end(foo_container), begin(bar_container), op_to_make_foo_into_bar);
edit: I see from your other response that what you actually want is streams or generators, not an iterator.
Thank you for demonstrating a better way to do it. Hopefully I'll remember that the next time that I need it. (Which is likely to be several years from now.)
Also you're right that I would like streams and generators. But, to me, it is the same abstraction as iteration. See, for instance, how Python does it.
Put another way, the effort of learning something needs to be amortized over all of the times that you'll likely use that knowledge. I use C++, rarely enough that I won't get much amortization. (And when I do use it, it is painful enough that I don't want to explore it just for personal enjoyment. Leading to my avoiding C++...)