[lambda] Initializing a std::list of unsigned short points

In my project I'm trying to initialize a std::list of unsigned short
pointers. I've created a couple of simple hand written loops but would
like to convert them to use std::for_each. I've been unsuccessful in
the past getting boost::lambda to work with std container of pointers.
In the lambda documentation I know that new_ptr and delete_ptr are need
in creating and destroying pointers in lambda functions. I've provided
the hand written loop that does work and the "attempted" conversion to
std::for_each. Any help on the correct implementation for the lambda
values would be appreciated.
Ryan
typedef std::list

AMDG Ryan McConnehey wrote:
In my project I'm trying to initialize a std::list of unsigned short pointers. I've created a couple of simple hand written loops but would like to convert them to use std::for_each. I've been unsuccessful in the past getting boost::lambda to work with std container of pointers. In the lambda documentation I know that new_ptr and delete_ptr are need in creating and destroying pointers in lambda functions. I've provided the hand written loop that does work and the "attempted" conversion to std::for_each. Any help on the correct implementation for the lambda values would be appreciated.
Ryan
typedef std::list
ml; for (ml::iterator itor = m_list.begin(); itor != m_list.end(); ++itor, startValue++) { *itor = new unsigned short(startValue); } for (ml::iterator itor = m_list.begin(); itor != m_list.end(); ++itor) { delete *itor; }
std::for_each(m_list.begin(), m_list.end(), *(bl::_1) = bl::new_ptr<unsigned short>()(bl::var(startValue)++) ); std::for_each(m_list.begin(), m_list.end(), boost::lambda::delete_ptr(*(boost::lambda::_1)));
Use bind. std::generate(m_list.begin(), m_list.end(), bl::bind(bl::new_ptr<unsigned short>(), bl::var(startValue)++); std::for_each(m_list.begin(), m_list.end(), bl::bind(bl::delete_ptr(), bl::_1)); In Christ, Steven Watanabe

std::generate(m_list.begin(), m_list.end(), bl::bind(bl::new_ptr<unsigned short>(), bl::var(startValue)++); std::for_each(m_list.begin(), m_list.end(), bl::bind(bl::delete_ptr(), bl::_1)); Thanks, that worked. Is there an advantage to using std::generate vs std::for_each for the assignment?
Ryan std::generate(m_list.begin(), m_list.end(), bl::bind(bl::new_ptr<unsigned short>(), bl::var(startValue)++)); vs. std::for_each(m_list.begin(), m_list.end(), bl::_1 = bl::bind(bl::new_ptr<unsigned short>(), bl::var(startValue)++));

Thanks, that worked. Is there an advantage to using std::generate vs std::for_each for the assignment?
std::for_each was originally defined as a non-modifying algorithm: http://www.open-std.org/jtc1/sc22/wg21/docs/lwg-defects.html#475
participants (3)
-
Igor R
-
Ryan McConnehey
-
Steven Watanabe