Jump to content

C++: Difference between revisions

439 bytes removed ,  20 October 2023
Line 471: Line 471:
====std::array====
====std::array====
<code>#include <array></code><br>
<code>#include <array></code><br>
In C++, you can use <code>std::vector</code> which gives you a resizable array.
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 or in a struct.
This will allocate an array in the heap.<br>
Unless you need stack allocation or allocation into a struct, you are should probably use a vector.
 
[https://shendrick.net/Coding%20Tips/2015/03/15/cpparrayvsvector.html array vs vector]<br>
If you need a statically allocated 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 manually allocate an 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====