Skip to content

Instantly share code, notes, and snippets.

@Yrds
Last active May 21, 2020
Embed
What would you like to do?
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