I have a hunch that this has been asked before, but I can't seem to find the answer. I have a struct similar to: struct MyStruct { int i; char c; }; And a functor similar to: struct MyFunctor { void operator()(int i) { //Do something with i } }; Given a container of MyStruct objects, how can I use iterator_adaptor to use MyStruct::i with MyFunctor? Something like: std::vector<MyStruct> v; std::for_each(v.begin(), v.end(), MyFunctor()); I have overridden the dereference method in default_iterator_policies to return the integer, but am at a bit of a loss where to go from here. Am I on the right track? TIA for your help! David Brownell
--- At Tue, 26 Mar 2002 17:18:20 -0800, David Brownell wrote:
I have a hunch that this has been asked before, but I can't seem to find the answer. I have a struct similar to:
struct MyStruct { int i; char c; };
And a functor similar to:
struct MyFunctor { void operator()(int i) { //Do something with i } };
Given a container of MyStruct objects, how can I use iterator_adaptor to use MyStruct::i with MyFunctor? Something like:
std::vector<MyStruct> v; std::for_each(v.begin(), v.end(), MyFunctor());
I have overridden the dereference method in default_iterator_policies to return the integer, but am at a bit of a loss where to go from here.
Am I on the right track?
Try taking a look at projection_iterator_adaptor or transform_iterator_adaptor in the documentation and examples for iterator_adaptor. I think the project adaptor is what you want. However, I have had problems getting this to work. Usually I end up with a strange typing problem associated with taking a reference; I havent spent the time to track down what is really happening. In those cases, I usually turn to the transform_iterator_adaptor. It usually works fine. ...Duane -- "If tyranny and oppression come to this land, it will be in the guise of fighting a foreign enemy." - James Madison
participants (2)
-
David Brownell
-
Duane Murphy