♦ 🐆 3 min, 🐌 7 min
C++
when I have to do some sort of massive data crunching. In any other case I turn to python
since I can write numeric code in it faster. But there are cases where C++
is the only way to get out alive. We'll mention some advance concepts here but mostly to make you as a novice aware that they exist. Because you always don't know what you still don't know and I'll try to bridge this gap a bit.C++
the C
way. Maybe they are stuck in the past or were just lazy to learn the cool features of modern C++
(C++ 17
). But this C
is C++
mantra is producing an army of coders that are not leveraging modern tools.I keep seeing code that uses double x[100];
for arrays instead of std::vector x(100);
. Or even worse: double *x = new double[100];
. Hack when I started coding I was doing the same thing. Then someone told me. Hey there's this thing cold STL
(C++
Standard Template Library) that has vectors that are smart. If you try to access an element that doesn't exist it will raise a warning. Cool that's worth exploring.If you use std::vector
instead of a double
array you have some pretty cool features at your disposal. You can enlarge the vector as the program gets executed, resize it, clean it up, ... To access elements you can use x.at(i)
instead of x[i]
sure the [i]
is faster but it doesn't check like .at(i)
if you are accessing elements that are not in the vector.CLion
it's the best Interactive Development Environment out there. It will tell you what you screwed up, remind you that you're maybe making some redundant copies of data, ... For the geeks. You can use vim
mode in it 😉*
pointers. Ever !!&
which you use when you don't want unnecessary copying. For example when passing data to a function:void my_calculator(const std::vector &x){
// implementation
}
const
will make sure that the function can't modify x
that you passed in. If you wouldn't pass in &x
but just x
the data would be copied for no reason.If you really have to use pointers then smart pointers. std::shared_ptr
or std::unique_ptr
are the answer. Sure there might be some small percentage of cases where the library you are using forces you to use raw pointers (BTW that's bad library design). In that case keep in mind that smart pointer is just a smart envelope around the raw pointer so you can always get the raw pointer 'out'. Example of packaging an arbitrary C++
object into a smart pointer would be:std::shared_ptr variable_name = std::make_shared
Instead of the raw way that you shouldn't use ever:double *x = new double [100];
Now why the hack is something like double *
so dangerous? Well if you delete the raw pointer to your data you will have pretty hard time getting back to it and cleaning it up. So before you realise your RAM is full and your PC can't perform any new calculations. In fancy words you cause a memory leak. By using smart pointers the smart pointers implementation sort of prevents you from doing that.stRG2
or sss
. The variable has to tell the reader descriptively what it represents. Good practice would be num_of_simulation_steps
for example.CLion
editor (it's free for students and academics for non commercial use) and cmake
build systemint main(){}
should be named main.cpp
that's usually good practice 🙂STL
(standard template library)STL
libraries for example: #include
from STL
instead of #include
using namespace std
sure you write a bit more code but you keep the option open to introduce functions with same namesGet notified & read regularly 👇