Mohan
Mohan

Reputation: 8863

"Mapping" a function over a set to create another set

In a functional language, if you have S, a set whose elements have type T, and f, a function : T -> U, you can write map f S (or some language-specific version of that) to create a set of Us.

Is it possible to achieve something similar in C++11 using for_each or some other STL constructs?

Upvotes: 1

Views: 59

Answers (1)

Igor Tandetnik
Igor Tandetnik

Reputation: 52611

std::set<U> SetOfUs;
std::transform(S.begin(), S.end(), std::inserter(SetOfUs, SetOfUs.begin()), f);

Upvotes: 3

Related Questions