What is the most recommended way to display an array in a function?
typedef int TVector[10];
with
const
:void MostrarArray(const TVector &v){} ^^^^^^^^^^^^^^^^
directly:
void MostrarArray(TVector v){} ^^^^^^^^^
Code
int main() {
TVector v;
MostrarArray(v);
return 0;
}
If the objective of
mostrarArray
is only to show its data on the screen, the correct option is constant reference (to avoid copies) (because it is not expected to be modified).The alias
TVector
you have used limits the use to static arrays of10
elements, but if you needed a functionmostrarArray
for static arrays of known size at compile time you could use this alternative:It seems to me that with the C++17 standard about to come out, and C++14 and C++11 already in full use, frankly, it is not convenient to dedicate more time and energy to the forms that were common more than 18 years ago. So I would tell you that one of the preferred ways in C++ is to use std::vector as the default container, unless you decide on something else (such as std::list, std::deque, std::array or others). ).
Using a std::vector, the simplest and most common way I think is