Last active
May 21, 2020 03:26
-
-
Save Yrds/6d6438c9bff9d2fdae5eb8ee4efd3959 to your computer and use it in GitHub Desktop.
Comparing C++ Array iteration with Javascript
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
const vect = [1,2,3,4,5]; | |
const newVect = vect.map(number => number + 1); //Sum all elements by 1 | |
console.log(newVect); // [2,3,4,5,6] |
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 <vector> | |
#include <algorithm> | |
#include <iostream> | |
int main(){ | |
std::vector<int> vec {1,2,3,4,5}; | |
std::vector<int> newVec; | |
std::transform(vec.begin(), vec.end(), std::back_inserter(newVec), | |
[](int number) -> int { return number + 1; }); //Sum all elements by 1 | |
for(auto number: newVec) | |
std::cout << number << " "; // 2 3 4 5 6 | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment