5 Quick Tips for C++
Mini Quick Reference Guide!
In today’s cpp::daily we’re going to talk about commonplace issues that take time searching the internet and often find not-so-good solutions for what we want!
There are 5 tips, but we will probably have other parts!
01. Print the variable type
To know the type of variable just include the header
#include <typeinfo> use
tydeid( var ).name(), example:
Remembering that the output will be the initial of the type name, for this example it will be:
double and so on. For some other types you will need to filter the output information.
02. Get your screen resolution
We know how to get this via the command line, but to use 100% C++ just include the
#include <X11/Xlib.h> lib, in this example for X.org
g++ resolution.cpp -lX11
03. Convert string to char
If you to search in Google this you will see a lot of complex solution worth that Khaby Lame meme.
But this can be done in a very simple way:
04. Converting string to integers
Speaking of converting, to convert integer types to string just use the STL itself, this is very useful when we are developing graphics applications. Examples:
05. Removing whitespace from a string
We know that many programming languages have the functions
ltrim()(remove left/start whitespace),
rtrim()(remove right/end whitespace) and
trim()(remove left/left whitespace/ start and right/end) .
Se nós usarmos aquela solução default do algorithm , provavelmente iremos remover não somente da esquerda e da direita como entre as letras/caracteres também, exemplo:
So to remove
trim() style would be:
'text with blank spaces'