Skip to content

Instantly share code, notes, and snippets.

@Yrds
Last active May 21, 2020 03:26
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 Yrds/6d6438c9bff9d2fdae5eb8ee4efd3959 to your computer and use it in GitHub Desktop.
Save Yrds/6d6438c9bff9d2fdae5eb8ee4efd3959 to your computer and use it in GitHub Desktop.
Comparing C++ Array iteration with Javascript
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]
#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