Static Arrays in C++ (std::array)

preview_player
Показать описание


Thank you to the following Patreon supporters:
- Dominic Pace
- Kevin Gregory Agwaze
- Sébastien Bervoets
- Tobias Humig
- Peter Siegmund
- Kerem Demirer

Gear I use:
-----------------

Рекомендации по теме
Комментарии
Автор

New drinking game: Take a shot everytime Cherno tells us he will cover it in a future video. :D

seditt
Автор

Doesn't really matter what the size is.

template<class T, size_t I>
void printArray(array<T, I> a)
{
for (auto &i : a)
{
cout << i << '\n';
}
}

int main()
{
array<int, 3> intArray {1, 2, 3};
array<double, 2> doubleArray {2.3, 5.5};
cout << "Printing Int Array" << '\n';
printArray(intArray);
cout << "Printing Double Array" << '\n';
printArray(doubleArray);

return 0;
}

LordVysh
Автор

It's worth noting that the size() function is also constexpr, so it doesn't actually return 5, but rather the compiler will just replace the function call itself with 5, so something like:

int s = ary.size();

literally becomes

int s = 5;

with no function call at runtime.

khatharrmalkavian
Автор

the best solution in my opinion is this:

template<int Length>
void PrintArray(const std::array<int, Length>& data) {
for(int i = 0; i < data.size(); ++i) {

}
}

StrtFght
Автор

The hardest part in C++ is deciding which thing to use

dXXPacmanXXb
Автор

template<int N>
void PrintArray(std::array<int, N>& arr)
{
for (int i = 0; i < arr.size(); i++)
{
std::cout << arr[i] << std::endl;
}
}
today I understood template finally. Thanks man.

imankalyanmaity
Автор

I think there is one crucial thing to note:

Bounds checking with std::array [] operator is not guaranteed in debug mode. If anyone wants to make sure that they have bounds checking, they should use the .at() instead of the [] operator to access array elements since bounds checking with .at() method is required by c++ standard, but it is not the case for the [] operator. Bounds checking is always enabled with .at() method, regardless of the build type of your program, meaning it's built into the program even when compiling in release mode with compiler optimization.
For MSVC, as @cherno has shown, the [] operator supports bounds checking and disables it during release builds. But for the compiler I am using, which is GCC, bounds checking is completely none-existent with [] operator.

Anyways, thanks for your awesome videos. I have learned a lot from your content and your videos are with all seriousness life-changing to me.

yusinwu
Автор

As most of the guys here have guessed, the correct way to print an std::array<T, N> is to use a template function.
Hohewer, I need to show off, so here's a function to print any iterable collection passing its begin() and end()
.
template <class Iterator>
void printCollection(Iterator first, Iterator last)
{
for (auto it = first; it < last; ++it) {
std::cout << *it << ' ';
}
}

expurple
Автор

"Stacks are stored in the arrays" - Cherno 2018. Well.. He *almost* said it.

Katniss
Автор

explanation to your question:
We use template to get the data and size

template<class T, size_t size>
void print( const std::array<T, size> & Data)
{
for ( T i : Data)
std::cout << i << std::endl;
}
int main()
{
std::array<int, 5> Number;
Number[0] = 10;
Number[1] = 12;
Number[2] = 13;
Number[3] = 19;
Number[4] = 0;

print(Number);

}

Sergeant_Mahdi
Автор

Have you considered making a tutorial on CMake? It might come in very useful for people looking to do cross-platform projects

TheHeadHunter
Автор

I think the best solution to the array size function parameter function is to make use of auto declarations as function arguments from C++20. The below compiles perfectly (tested on gcc):

#include <iostream>
#include <array>

void PrintArray(const auto& data)
{
std::cout << "[";
for (unsigned i = 0; i < data.size(); i++){
std::cout << data[i];
if (i == data.size() - 1)
std::cout << "]";
else
std::cout << ", ";
}
std::cout << std::endl;
}

int main()
{
std::array <int, 5> data;
data = {0, 5, 9, 8, 6};
PrintArray(data);
return 0;
}

/*
To compile:
$ g++ -o array.bin -Wall -std=c++20 array.cpp
$ ./array.bin
[0, 5, 9, 8, 6]
$

*/

kzm
Автор

you can use initializer list just like array:
eg:
std::array<int, 5> arr{1, 2, 3, 4, 5};
or:
std::array<int, 5> arr = {1, 2, 3, 4, 5};

koungmeng
Автор

Great!! so sensible. I would like to add two point, hope you will be aligned with me.
1. Usage of static_assert and assert Functions with std::array:
static_assert and other assertion functions work effectively with std::array because its result is a compile-time constant, and conditions are evaluated at compile time.
Since std::array has a fixed size known at compile time, it is suitable for use in compile-time checks like static_assert.

2. Applicability of constexpr with std::array and std::vector:
constexpr can be effectively used with std::array as it is resolved at compile time and its size is known at compile time.
On the other hand, std::vector is a runtime container, and its size is determined at runtime. Therefore, it is not suitable for use in constexpr contexts.

AdityaKumar-xwyx
Автор

awesome series.
you should edit out the "challenges". not many watches youtube guides to be told to google or find another vid explaining the details of the vid you are watching to learn the details ;)
but awesome c++ series ^^

Erebus
Автор

template <typename T>
void PrintArray(T& a)
{
for (int i = 0; i < a.size(); i++)
{
std::cout << a[i] << std::endl;
}
}

TuanAnh-ojps
Автор

using gsl::span<T> of the Guidelines Support Library (is a Microsoft implementation of some of the types and functions described in the C++ Core Guidelines)

DrAuraHxC
Автор

Without checking if this compiles, I think the answer to the question at 4:00 is

template <int size>
void PrintArray(const array<int, size>& data) {
// Do Stuff
}

Amirite?

ToyMachine
Автор

template<typename array_type>
void PrintArray(const array_type& m_array)
{
cout << "Array Contents:" << endl;
for (auto& i : m_array)
{
cout << i << endl;
}
}

KingCitaldo
Автор

template<class T, int n>
void print(array<T, n>&array){
int size = array.size();
for (int i = 0; i < size; i++){
cout << array[i] << endl;
}


}

int main(){

array<float, 5> data1;
data1[0] = 1.1f;
data1[1] = 100.2f;
data1[2] =200.3f;
data1[3] = 400.4f;
data1[4] = 30.5f;
print(data1);
system("pause");
return 0;
}

vikramkumarbathala