4 ms·
> For example suppose that I have a class that has reference members. Those are fields specified with & - they are kind of like a pointer except that you can't
by compusa 14y ago
> For example suppose that I have a class that has reference members. Those are fields specified with & - they are kind of like a pointer except that you can't change them, and you use . instead of ->. Because you can't change them, operator= is not generated for you. I didn't notice this until I tried to use one in an std::map< ... > and everything blew up.
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.
- btilly 14y agoAs I said, my C++ experience is limited. 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.
- marblar 14y agoYou should check out the boost iterator library. Depending on the application, you might be looking for either the function input iterator or the counting iterator.
- btilly 14y agoI have avoided learning boost for the simple reason that on the rare occasions when I need C++, it has been apparent to me that I could easily spend longer trying to evaluate whether anything that is available would be a good fit for my problem, than it would take to be done. 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++...)