Skip to content

Instantly share code, notes, and snippets.

@JohnMurray
Created July 3, 2020 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save JohnMurray/6b35f7acc93a4da96ed8431347ee9026 to your computer and use it in GitHub Desktop.
Save JohnMurray/6b35f7acc93a4da96ed8431347ee9026 to your computer and use it in GitHub Desktop.
#include <algorithm>
#include <functional>
#include <vector>
int main() {
std::function<int(int)> addOne = [](int x) -> int {
return x + 1;
};
addOne(5);
// returns 6
// use lambda as value, do in-place modification
std::vector<int> v = {1, 2, 3, 4, 5};
std::transform(v.begin(), v.end(), v.begin(),
addOne);
// new value of 'v' is [2, 3, 4, 5, 6]
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment