Learning C++ as a Javascript developer
Software Engineer @ Google
Twitter: @charliesbot
Podcast: Los Full Stackers
C++98
What I learned at School
C++17
What I'm learning at Google
void pokeball() {
Pokemon* bulbasaur = new Pokemon();
Pokemon* charmander = new Pokemon();
Pokemon* squirtle = new Pokemon();
capture(*bulbasaur, *charmander, *squirtle);
// use bulbasaur
// use charmander
// use squirtle
// ...
delete bulbasaur;
delete charmander;
delete squirtle;
}
void pokeball() {
Pokemon* bulbasaur = make_unique<Pokemon>();
Pokemon* charmander = make_unique<Pokemon>();
Pokemon* squirtle = make_unique<Pokemon>();
capture(*bulbasaur, *charmander, *squirtle);
// use bulbasaur
// use charmander
// use squirtle
// ...
}
int num = 1;
string text = "hello word";
vector<int> nums = {1, 2, 3, 4};
unordered_map<char, char> dict(
{
{'(', ')'},
{'[', ']'},
{'{', '}'}
}
);
const num: number = 1;
const text: string = "hello word";
const nums: number[] = [1, 2, 3, 4];
const hash: {[key: string]: string} =
{
'(': ')',
'[': ']',
'{', '}',
};
auto num = 1;
auto text = "hello word";
vector<auto> nums = {1, 2, 3, 4};
unordered_map<auto, auto> dict(
{
{'(', ')'},
{'[', ']'},
{'{', '}'}
}
);
const num = 1;
const text = "hello word";
const nums = [1, 2, 3, 4];
const hash = {
'(': ')',
'[': ']',
'{', '}',
};
Cool cool... but what about map/filter/reduce?
Loops are for cavemen
vector<int> numbers = {1, 2, 3, 4, 5};
vector<int> even_numbers;
copy_if(
numbers.begin(),
numbers.end(),
back_inserter(even_numbers),
[](int i){ return i % 2 == 0}
);
Filter
vector<int> numbers = {1, 2, 3, 4, 5};
vector<int> doubled_numbers;
transform(
numbers.begin(),
numbers.end(),
back_inserter(doubled_numbers),
[](int i){ return i * 2 == 0}
);
Map
vector<int> numbers = {1, 2, 3, 4, 5};
int result = accumulate(
numbers.begin(),
numbers.end(),
[](int i, int b){ return a + b}
);
Reduce
Twitter: @charliesbot
Podcast: Los Full Stackers