Jump to content

C++: Difference between revisions

391 bytes added ,  26 November 2019
no edit summary
No edit summary
(6 intermediate revisions by the same user not shown)
Line 1: Line 1:
__FORCETOC__
__FORCETOC__
C++ is a very popular and powerful language which includes all the low-level features of [[C_(programming_language) | C]] (e.g. pointers, operator overloading) along many high-level features (regex, STL containers) thanks to the C++ standard library.<br>
Some people may think of it as an object-oriented version of C.


==Usage==
==Usage==
Line 50: Line 52:
This will allocate an array in the heap.<br>
This will allocate an array in the heap.<br>


[https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html array vs vector]
[https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html array vs vector]<br>
If you need a static sized array, you can use <code>std::array</code> in the <code>array</code> header.<br>
If you need a static sized array, you can use <code>std::array</code> in the <code>array</code> header.<br>
This wrapper around C-style arrays gives us size information and allows the array to be passed around by reference while keeping the array on the stack unlike <code>std::vector</code>.<br>
This wrapper around C-style arrays gives us size information and allows the array to be passed around by reference while keeping the array on the stack unlike <code>std::vector</code>.<br>
Line 240: Line 242:
std::numeric_limits<float>::max();
std::numeric_limits<float>::max();
</syntaxhighlight>
</syntaxhighlight>
==Programming Styles==
===Modern C++===
[https://github.com/rigtorp/awesome-modern-cpp List of resources]<br>
Prefer newer std functions available in C++17.<br>
Use shared pointers instead of new and delete.<br>
* Use clang-format.
===Orthodox C++===
[https://gist.github.com/bkaradzic/2e39896bc7d8c34e042b Reference]<br>
Somewhat opposite of modern C++.<br>
Basically only use C++ for its classes. Do everything else C-style.
The main benefit is compatibility with older compilers/libraries and easier understanding for people less familiar with newer C++ features.
*Don't use C++ runtime wrapper for C runtime includes (<cstdio>, <cmath>, etc.), use C runtime instead (<stdio.h>, <math.h>, etc.)
*Don't use stream (<iostream>, <stringstream>, etc.), use printf style functions instead.
*Don't use anything from STL that allocates memory, unless you don't care about memory management.


==STL==
==STL==
Line 262: Line 248:
Many STL containers are now built into the standard library (std) of C++.<br>
Many STL containers are now built into the standard library (std) of C++.<br>
This section focuses only on the portions of STL which have been incorporated into the C++ standard library.
This section focuses only on the portions of STL which have been incorporated into the C++ standard library.
==Containers==


===Simple Containers===
===Simple Containers===
Line 290: Line 275:


==Boost==
==Boost==
==Programming Styles==
===Modern C++===
[https://github.com/rigtorp/awesome-modern-cpp List of resources]<br>
Prefer newer std functions available in C++17.<br>
Use shared pointers instead of new and delete.<br>
* Use clang-format.
===Orthodox C++===
[https://gist.github.com/bkaradzic/2e39896bc7d8c34e042b Reference]<br>
Somewhat opposite of modern C++.<br>
Also known as "C with Classes"<br>
Basically only use C++ for its classes. Do everything else C-style.<br>
The main benefit is compatibility with older compilers/libraries and easier understanding for people less familiar with newer C++ features.
* Don't use C++ runtime wrapper for C runtime includes (<cstdio>, <cmath>, etc.), use C runtime instead (<stdio.h>, <math.h>, etc.)
* Don't use stream (<iostream>, <stringstream>, etc.), use printf style functions instead.
* Don't use anything from STL that allocates memory, unless you don't care about memory management.
* Don't use exceptions.
* Don't use RTTI.


==Useful Libraries==
==Useful Libraries==