C++: Difference between revisions

32 bytes added ,  7 October 2019
no edit summary
No edit summary
No edit summary
Line 1: Line 1:
__FORCETOC__
__FORCETOC__


=Usage=
==Usage==
How to do things using the [https://en.wikipedia.org/wiki/C%2B%2B_Standard_Library C++ standard library (stdlib)].
How to do things using the [https://en.wikipedia.org/wiki/C%2B%2B_Standard_Library C++ standard library (stdlib)].
==Compilation==
===Compilation===
===G++===
====g++====
<syntaxhighlight lang="bash">
<syntaxhighlight lang="bash">
g++ my_driver.c [-Iincludefolder] -o my_program.out
g++ my_driver.c [-Iincludefolder] -o my_program.out
Line 11: Line 11:
* <code>-std=c++17</code> for C++17 support
* <code>-std=c++17</code> for C++17 support
* <code>-O3</code> for level 3 optmizations
* <code>-O3</code> for level 3 optmizations
==Strings==
===Strings===
===String Interpolation===
====String Interpolation====
[https://stackoverflow.com/questions/10410023/string-format-alternative-in-c Reference]
[https://stackoverflow.com/questions/10410023/string-format-alternative-in-c Reference]
<syntaxhighlight lang="C++">
<syntaxhighlight lang="C++">
Line 29: Line 29:
}
}
</syntaxhighlight>
</syntaxhighlight>
==Filesystem==
===Filesystem===
===Reading and Writing===
====Reading and Writing====
Reading and writing is done using <code>fstream</code>.<br>
Reading and writing is done using <code>fstream</code>.<br>
If you don't need r/w, use <code>istream</code> for reading or <code>ostream</code> for writing.<br>
If you don't need r/w, use <code>istream</code> for reading or <code>ostream</code> for writing.<br>
Line 49: Line 49:
</syntaxhighlight>
</syntaxhighlight>


==Regular Expressions==
===Regular Expressions===
[https://en.cppreference.com/w/cpp/regex Reference]
[https://en.cppreference.com/w/cpp/regex Reference]
<!--
<!--
Line 63: Line 63:
-->
-->


==Threading==
===Threading===
=== Sleep ===
==== Sleep ====
<syntaxhighlight lang="C++">
<syntaxhighlight lang="C++">
std::this_thread::sleep_for(std::chrono::milliseconds(1));
std::this_thread::sleep_for(std::chrono::milliseconds(1));
</syntaxhighlight >
</syntaxhighlight >


==Memory==
===Memory===
=== Garbage Collection ===
==== Garbage Collection ====
Traditional C++ does not have garbage collection.   
Traditional C++ does not have garbage collection.   
After using `new` to allocate an object, use `delete` to deallocate it.   
After using `new` to allocate an object, use `delete` to deallocate it.   
Line 78: Line 78:




=Programming Styles=
==Programming Styles==
==Modern C++==
===Modern C++===
[https://github.com/rigtorp/awesome-modern-cpp List of resources]<br>
[https://github.com/rigtorp/awesome-modern-cpp List of resources]<br>
Prefer newer std functions available in C++17.<br>
Prefer newer std functions available in C++17.<br>
Line 93: Line 93:
*Don't use anything from STL that allocates memory, unless you don't care about memory management.
*Don't use anything from STL that allocates memory, unless you don't care about memory management.


=Boost=
==Boost==
=STL=
==STL==