Skip to content

Instantly share code, notes, and snippets.

@ryohji
Created January 21, 2022 16:33
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 ryohji/f36da34908cf239b96af027f973bd7ab to your computer and use it in GitHub Desktop.
Save ryohji/f36da34908cf239b96af027f973bd7ab to your computer and use it in GitHub Desktop.
Use transform(map) function to modify the elements in a container. [C++]
#include <algorithm>
#include <iostream>
#include <iterator>
#include <vector>
int main() {
auto v = std::vector<int> {1, 2, 3};
// Use transform to update all elements in the vector.
std::transform(std::cbegin(v), std::cend(v), std::begin(v),
[](int v) { return 2 * v; });
// Print elements; resulted: 2 4 6 .
std::copy(std::cbegin(v), std::cend(v),
std::ostream_iterator<int>(std::cout, " "));
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment