jeudi 1 mars 2018

Some tips in C++

In some exercises you just need a great algorithm but if you think your code is already the best of the best then you can use these tricks :

- A way to avoid writing std:: everywhere is to write : using namespace std;

- Don't use endl but use '\n' instead

- Use these three lines of code in the beginning of your code for quicker input / output :  
  • ios::sync_with_stdio(false); 
  • cin.tie(nullptr);
  • cout.tie(nullptr); 

- Use v.emplace_back(1); instead of v.push_back(1);
- Use nullptr instead of 0
- Use auto
- Use modern C++
- Use inline in front of each function
- Declare global variables
- Use less parameters in functions
- Use list for constant erse and insert
- Use vector for constant access to each and every element of a vector
- Use set to erase duplicate and sort a set of elements
- Use unordered_set to erase quicker the duplicate elements than set
- Use multi_set to sort a set of elements quicker the elements than set
- Use unordered_multiset when needed (not explained)
- Use map and unordered_map and multi_map and unordered_multimap for questions needing dictionary

If all of that is done then I think the last thing you can do is translating your code into C code, it will still compile in C++. If you have any questions you can ask me !

Aucun commentaire:

Enregistrer un commentaire

Poem : Windrush Generation

Windrush Generation Past glory of England Succes of this Nation Based on the strength of this land Many Jamaicans Find work, prosperity Have...