STL name for the "map" functional programming function
This question was asked before the C++11 standard went into effect... nowadays we have std::transform()
as the (ugly) equivalent of a functional programming 'map'. Here's how to use it:
auto f(char) -> char; // or if you like: char f(char)
vector<char> bar;
vector<char> foo;
// ... initialize bar somehow ...
std::transform(bar.begin(), bar.end(), std::back_inserter(foo), f);
You can use std::back_inserter
in <iterator>
, although providing the size in front is more efficient. For example:
string str = "hello world!", result;
transform(str.begin(), str.end(), back_inserter(result), ::toupper);
// result == "HELLO WORLD!"