Created
January 21, 2022 16:33
-
-
Save ryohji/f36da34908cf239b96af027f973bd7ab to your computer and use it in GitHub Desktop.
Use transform(map) function to modify the elements in a container. [C++]
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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