The power of C++
Learning C++ as a Javascript developer
Charlie L
Software Engineer @ Google
Twitter: @charliesbot
Podcast: Los Full Stackers
As a javascript developer, I've always heard this...
No one
USES
C++
Then i joined google...
And it was all about C++
if your last attempt to try c++ happened at school...
C++ has changed
C++98
What I learned at School
C++17
What I'm learning at Google
Why, AS JS Developers, we think C++ Sucks
- Manual memory allocation: new, delete, etc
- Types
- Where are my map, filter and reduce?
Memory allocation
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
// ...
}
TYPES
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
the power of modern c++
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
so, is C++ still a thing?
yes.
- It's a solid and mature language
- There's tons of libraries
- It's efficient and performant!
- It has a huge community
Thanks!
Twitter: @charliesbot
Podcast: Los Full Stackers
The power of C++
By Carlos Lopez
The power of C++
- 694