Monday, July 20, 2009

Calling a member function for all STL map values (in a single line of code)


int newSize = 50;
typedef std::map<int, vector<double>> MyMap;

// call vector.resize(newSize) for all values (pair->second) of STL map
for_each(m_map.begin(), m_map.end(),
   tr1::bind(
     mem_fun_ref(&vector<double>::resize),
        tr1::bind(&MyMap::value_type::second, tr1::placeholders::_1),
        newSize));

No comments: