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

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

The Cherno

6 лет назад

108,135 Просмотров

Ссылки и html тэги не поддерживаются


Комментарии:

@jovialcupid9687
@jovialcupid9687 - 13.12.2023 10:22

Challenge accepted:

Use for each or just calculate size of the array. I'm I stupid and I'm not understanding something or what?

Void printArray( int* arr)
{
for (auto i: arr) serial.println(i);

// or

For (int I = 0; I < sizeof(arr) / sizeof(int); i++)
//Int is default cuz this is parameter type, u can make template out of it
}

Ответить
@sunstrumsharam5388
@sunstrumsharam5388 - 11.12.2023 07:07

I wrote that:
template<int T>
void PrintArray(std::array<int, T>& arr)
{
for (int i = 0; i < arr.size(); i++)
{
std::cout << arr[i] << '\n';
}
}

Ответить
@fireballgfx
@fireballgfx - 05.12.2023 15:50

template<int S,typename T> void foo(std::array<T,S>){...}

Ответить
@VanYang-eq5ub
@VanYang-eq5ub - 26.11.2023 15:15

it totally can work pretty well。

Ответить
@VanYang-eq5ub
@VanYang-eq5ub - 26.11.2023 15:14

template<class T,int sizee>
void func(const std::array<T,sizee>& arr) {
cout << arr.size();
}

Ответить
@milosorevic927
@milosorevic927 - 15.11.2023 17:21

void PrintArray(const auto& data) {
for (const auto& value : data) {
std::cout << value << " ";
}
std::cout << std::endl;
}

or

template <typename T, size_t N>
void PrintArray(const std::array<T, N>& data) {
for (const T& value : data) {
std::cout << value << " ";
}
std::cout << std::endl;
}


edit: after C++17 and later you can use auto, however you lose some type safety and intent clarity, as well as being able to tell error messages correctly. I prefer the template way, however both are correct.

Ответить
@OhmicContact
@OhmicContact - 07.11.2023 21:48

35 degrees is cold bro

Ответить
@paragggoyal1552
@paragggoyal1552 - 12.10.2023 01:59

but the issue is we should know the size at compile time, if we don't know the size at compile time, we cannot specify the size in template argument.

Ответить
@earthian2777
@earthian2777 - 27.06.2023 15:18

The array size problem can be solved using templates

Ответить
@zoro-i
@zoro-i - 24.06.2023 05:21

bro how is it hot its Jan u should be freezing

Ответить
@Sergeant_Mahdi
@Sergeant_Mahdi - 30.05.2023 07:01

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);

}

Ответить
@perajarac
@perajarac - 19.04.2023 15:02

hope u got aircondition after 5 years of hard work

Ответить
@phantomstriker7996
@phantomstriker7996 - 17.01.2023 01:48

Static arrays (std::array) are just an array which you can't change the value of and size of.

Ответить
@vectoralphaAI
@vectoralphaAI - 22.09.2022 21:02

Yeah a huge problem with C++ programmers is that the majority of them are not programming in C++, but instead in C without even knowing it. If you're writing C++ then write C++ code, otherwise just go and write C. No one in C++ should be creating C style arrays, youre using C++ so do it the C++ way and use std::Array instead.

Ответить
@stavroscho7798
@stavroscho7798 - 20.09.2022 20:40

You can set the size with a template

Ответить
@Popart-xh2fd
@Popart-xh2fd - 10.09.2022 00:14

The solution is auto, like explained in the previous video! So, "const auto& data"!

Ответить
@idc20627
@idc20627 - 04.05.2022 02:43

Going over bounds in a c-style array gave me seg fault core dump

Ответить
@Deeredman4
@Deeredman4 - 20.04.2022 23:25

I think that if you don't know the size, you can either use a template or a macro but I think you have explicitly said that macros are mostly bad except for debugging and too much templating can become dubious. Based on my current knowledge I would probably use a template.

Ответить
@serhat4571
@serhat4571 - 15.04.2022 01:55

template<typename T, size_t X>
void PrintArray(const std::array<T, X>& array)
{
for(int i = 0; i < X; i++)
{
std::cout << array[i] << std::endl;
}
}

Ответить
@gniludio
@gniludio - 22.03.2022 19:19

template <std::size_t SIZE>
void PrintArray(const std::array<int, SIZE>& array) {
for (std::size_t i = 0; i < SIZE; i++) {
std::cout << array[i] << std::endl;
}
}

Ответить
@ml-tech
@ml-tech - 16.03.2022 04:57

I don’t think std::arrays do bounds checking using the overloaded [] operator. You would have to use the `at(i)` method instead of [i].

Ответить
@skewbinge6157
@skewbinge6157 - 13.02.2022 17:13

templates right away answer thanks to you man

Ответить
@puppergump4117
@puppergump4117 - 01.01.2022 08:23

If you pass in an std::array then you should be able to just get the size from that right

Ответить
@maga8289
@maga8289 - 04.11.2021 16:56

You can pass the address of std::array as void* ptr in function argument and by knowning the offset of size inside array class you can find it like this: int size = *(int*)(uintptr_t(ptr) + *size_offset*)

Ответить
@user-jm7jh7hv5h
@user-jm7jh7hv5h - 12.10.2021 08:07

Thank you, i am grateful that there is person such you. It really helps to figure out in c++

Ответить
@khatharrmalkavian3306
@khatharrmalkavian3306 - 22.08.2021 20:08

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.

Ответить
@khatharrmalkavian3306
@khatharrmalkavian3306 - 22.08.2021 20:01

To pass an unsized std::array to a function the function must be templated with the size parameter as a template parameter.

Which is pretty gross because it forces your code into the header and makes a lot of things rigid when you're using it as a class member, ets. This is the main reason that I usually don't bother with std::array and just go with a vector instead, even if the size is constant.

Ответить
@khatharrmalkavian3306
@khatharrmalkavian3306 - 22.08.2021 19:59

Use size_t for sizes, you heathen.

Ответить
@tralamysamy8465
@tralamysamy8465 - 18.08.2021 05:35

The best solution with C++ 20 ➡
void PrintArray (const std::span<int>& data) {
for (auto& : data) {
std::cout << data << std::endl;
}

// Or

for (int i = 0; i < data.size(); i++) {
std::cout << data[i] << std::endl;
}
}

Ответить
@pierfrancescopeperoni
@pierfrancescopeperoni - 16.07.2021 13:40

I looked at the date 18 January.
Then you said that it's 40 degrees outside.
Then I remembered that you live in Australia.

Ответить
@meer_kat5158
@meer_kat5158 - 27.06.2021 09:27

Hi, if std::array is stored on stack, how is this code valid?

std::array<int, 3> f() {
return std::array<int, 3> {1, 2, 3};
}

Ответить
@aryanwolfox6390
@aryanwolfox6390 - 25.06.2021 13:16

Answer is by template :
template<typename T, std::size_t size>
void printArray(const std::array< T, size >&data)
{
// now we don’t need to pass type and size explicitly
}

Ответить
@vivekkumar-bq2ln
@vivekkumar-bq2ln - 20.06.2021 16:42

Isn't adding too many std::array with varying sizes (say for the same data type) will increase the size of program memory (code segment)? The size stored as a literal in the compiled program and that template is for every size type at the cost of program size.

Ответить
@TheMrsansari
@TheMrsansari - 15.05.2021 18:24

Im aiming to be a coder. Started this year. I wonder how long it will take me to get my first job?

Ответить
@kltf34cgsdawe9
@kltf34cgsdawe9 - 14.05.2021 08:31

auto or template

Ответить
@labelsmonkey
@labelsmonkey - 22.04.2021 12:23

i'm screwed. i dislike the russian captions. help me. its so annoying

Ответить
@labelsmonkey
@labelsmonkey - 22.04.2021 12:18

It's weired .why the captions were Russian.
?Omg, is google brain smashed?
Cherno help me. I wanna English captions.

Ответить
@cisimon7942
@cisimon7942 - 13.04.2021 12:33

late to the party, but I'd go about the problem as below:

template<int size>
void printArray(std::array<int, size>& data) {
for (int i = 0; i <data.size(); ++i) {
LOG(data[i]);
}
}



int main() {
std::array<int, 5> data;
...


printArray<data.size()>(data);
...

}

Ответить
@aerahtv0000
@aerahtv0000 - 27.03.2021 21:43

so i came back from stack overflow and looks like you can't pass this shit::array to function without specifying size. You either have to declare function as a template or specify size in every function, which basically same tracking as with c-array, so what's the point? Just another burden crap

Ответить
@avocadopeel7536
@avocadopeel7536 - 19.03.2021 18:11

To the people suggesting templates with std::array, why not

template<int N>
void PrintArray(int *array) {
for(int i=0; i<N; i++) {

}
}

Ответить
@cactusmamelu313
@cactusmamelu313 - 05.03.2021 05:52

Forty degree, finally a sane person that doesn't use square feet by crate inch type of unit !

Ответить
@CriticRoshun
@CriticRoshun - 03.03.2021 18:21

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

Ответить
@mattgraves3709
@mattgraves3709 - 31.01.2021 08:40

Man, that heat lol

Ответить
@RWM_
@RWM_ - 28.01.2021 13:36

At the beginning you're like Eric Andre, but.. but we missed whole intro!

Ответить
@KingCitaldo125
@KingCitaldo125 - 28.12.2020 19:42

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

Ответить