for_each on boost::ptr_map

Hi, Is there a way I can use std::for_each() on a boost::ptr_map and have the functor object be given the *value* of each item iterated instead of a key/value pair like it does with std::map? Help is appreciated, thank you.

Robert Dailey skrev:
Hi,
Is there a way I can use std::for_each() on a boost::ptr_map and have the functor object be given the *value* of each item iterated instead of a key/value pair like it does with std::map? Help is appreciated, thank you.
You have wrap the iterators in new iterators that project the pair-like structur down to the value. It's the same for std::map. You can find functions for that in Neil Groves range_ex library in the file vault. -Thorsten

On Mon, Sep 15, 2008 at 9:20 PM, Robert Dailey
Hi,
Is there a way I can use std::for_each() on a boost::ptr_map and have the functor object be given the *value* of each item iterated instead of a key/value pair like it does with std::map? Help is appreciated, thank you.
std::for_each() with std::map receives the pair so you need something
like the following to extract the value:
#include <algorithm>
#include <iostream>
#include <map>
#include <string>
#include

On Tue, Sep 16, 2008 at 3:00 PM, Peter Barker
On Mon, Sep 15, 2008 at 9:20 PM, Robert Dailey
wrote: Hi,
Is there a way I can use std::for_each() on a boost::ptr_map and have the functor object be given the *value* of each item iterated instead of a key/value pair like it does with std::map? Help is appreciated, thank you.
std::for_each() with std::map receives the pair so you need something like the following to extract the value:
#include <algorithm> #include <iostream> #include <map> #include <string>
#include
class MyClass { public: MyClass(int x) : x_(x) {} int getX() const {return x_;} private: int x_; };
void func(MyClass* mc) { std::cout << mc->getX() << std::endl; }
int main() { typedef std::mapstd::string,MyClass* MapType; MapType myPtrMap;
std::string key1 = "Key1"; std::string key2 = "Key2"; myPtrMap.insert(std::make_pair(key1,new MyClass(1))); myPtrMap.insert(std::make_pair(key2,new MyClass(2))); std::for_each(myPtrMap.begin(),myPtrMap.end(),boost::bind(&func,boost::bind(&MapType::value_type::second,_1))); }
Here's the same code working with ptr_map:
#include <algorithm> #include <iostream> #include <string>
#include
#include class MyClass { public: MyClass(int x) : x_(x) {} int getX() const {return x_;} private: int x_; };
void func(MyClass* mc) { std::cout << mc->getX() << std::endl; }
int main() { typedef boost::ptr_mapstd::string,MyClass MapType; MapType myPtrMap;
std::string key1 = "Key1"; std::string key2 = "Key2"; myPtrMap.insert(key1,new MyClass(1)); myPtrMap.insert(key2,new MyClass(2)); std::for_each(myPtrMap.begin(),myPtrMap.end(),boost::bind(&func,boost::bind(&MapType::value_type::second,_1))); }
I hope that helps you out.
Regards,
Pete _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
At risk of sounding a complete wally, I have completely failed to understand why the second version is any way preferrable to the first. Why would you want to do this? - Rob.

At risk of sounding a complete wally, I have completely failed to understand why the second version is any way preferrable to the first.
Why would you want to do this?
- Rob.
Are you asking why you would you use a boost::ptr_map over a std::map? I believe it's so the container takes ownership of the pointed to objects. http://www.boost.org/doc/libs/1_35_0/libs/ptr_container/doc/ptr_container.ht... contains this line: "For each of the standard containers there is a pointer container equivalent that takes ownership of the objects in an exception safe manner. In this respect the library is intended to solve the so-called polymorphic class problem." Regards, Pete

On Tue, Sep 16, 2008 at 3:25 PM, Peter Barker
At risk of sounding a complete wally, I have completely failed to understand why the second version is any way preferrable to the first.
Why would you want to do this?
- Rob.
Are you asking why you would you use a boost::ptr_map over a std::map? I believe it's so the container takes ownership of the pointed to objects. http://www.boost.org/doc/libs/1_35_0/libs/ptr_container/doc/ptr_container.ht... contains this line:
"For each of the standard containers there is a pointer container equivalent that takes ownership of the objects in an exception safe manner. In this respect the library is intended to solve the so-called polymorphic class problem."
Regards,
Pete _______________________________________________ Boost-users mailing list Boost-users@lists.boost.org http://lists.boost.org/mailman/listinfo.cgi/boost-users
Ah I see,.... so the code for ptr_map is intended to be as much like the code for std::map as possible! Got it now, thanks. - Rob. -- ACCU - Professionalism in programming - http://www.accu.org

Ah I see,.... so the code for ptr_map is intended to be as much like the code for std::map as possible! Got it now, thanks.
- Rob.
I think they were designed with that ease of transition in mind. I've never used them before so probably not the best person to answer but good to talk to someone about them as it helps us all to learn! The main benefit seems to be they delete the pointed to objects when the container goes out of scope. A std::map would not attempt to do this. Regards, Pete
participants (4)
-
Peter Barker
-
Robert Dailey
-
Robert Jones
-
Thorsten Ottosen