Jump to content

C++: Difference between revisions

47 bytes added ,  8 January 2020
(2 intermediate revisions by the same user not shown)
Line 46: Line 46:
* <code>dynamic_cast</code>
* <code>dynamic_cast</code>
If you're casting between things but do not want to change the bit-pattern (e.g. binary data or pointers), you can also use <code>reinterpret_cast</code>.
If you're casting between things but do not want to change the bit-pattern (e.g. binary data or pointers), you can also use <code>reinterpret_cast</code>.
===Array===
<code>#include <array></code><br>
In C++, you can use <code>std::vector</code> which gives you a resizable array.
This will allocate an array in the heap.<br>
[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>
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>
If you want to allocate a static array on the heap, you can do so as follows:
<syntaxhighlight lang="C++">
auto my_arr = std::make_shared<std::array<char,64>>();
</syntaxhighlight>


===String===
===String===
Line 285: Line 272:


===Sequences===
===Sequences===
====std::array====
<code>#include <array></code><br>
In C++, you can use <code>std::vector</code> which gives you a resizable array.
This will allocate an array in the heap.<br>
[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>
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>
If you want to allocate a static array on the heap, you can do so as follows:
<syntaxhighlight lang="C++">
auto my_arr = std::make_shared<std::array<char,64>>();
</syntaxhighlight>
====std::vector====
====std::vector====
[https://en.cppreference.com/w/cpp/container/vector Reference]<br>
[https://en.cppreference.com/w/cpp/container/vector Reference]<br>
Line 313: Line 314:
my_vec.back();
my_vec.back();
</syntaxhighlight>
</syntaxhighlight>
====std::deque====
Double-ended queue


====std::list====
====std::list====