c++ - C++11 How to use lambda and higher order functions to transform a vector to a vector of different type -
is there way return vector of different type using higher order functions in c++11? have std::vector<std::string> , want convert std::vector<foo> enum of own device.
assume have method foo tofoo(std::string). i've tried:
std::vector<foo> m_foos = std::for_each(m_foo_strings.begin(), m_foo_strings.end(), [this](std::string &s){ tofoo(s); } ); and tried:
std::vector<foo> m_foos = std::transform(m_foo_strings.begin(), m_foo_strings.end(), m_foo_strings.begin(), [this](std::string &s){ tofoo(s); } ); but neither compiles. complains no operator= defined std::string foo.
there has common way i'm trying here, missing?
std::transform not return vector, applies specified transformation , stores results in destination range pass it
std::transform(m_foo_strings.begin(), m_foo_strings.end(), std::back_inserter(m_foos), [this](std::string &s){ return tofoo(s); } ); back_inserter constructs back_insert_iterator call vector::push_back add elements m_foos when transform assigns result of call lambda.