I really appreciate the approach you've taken here with introducing STL concepts early on, especially in such a natural way. This often feels like a rushed after thought in other tutorials I've watched.
Hi Mike! Thanks for this great C++ series! The only one minor issue I've noticed in this video: I've learned from Jason Turner's videos, that calling std::endl in a loop is considered as a bad practice. He advised to use ' ' instead, and if one really wants to flush the buffer so it would be better to use std::flush outside the loop.
Hello Mike, for the record, I'm brazilian and I'm really enjoying your lessons! As for this one, a point raised me a doubt bc when you assigned a value outside the range previously set up to 'Ids' array, you received a 'segmentation fault' message, but in my case I didn't receive anything and it compiled well and even was able to execute. Do you know the reason of this behavior?
You can always make a raw array 'static' to zero-initialize it, as that memory needs to be reserved. I haven't covered 'constinit' yet, but that can be helpful to gaureentee static initialization. Otherwise, the use of 'constexpr' can shine here for compile-time execution, and filling in your array :) Perhaps this is a separate video in the future.
Hey Mike, really good video! Question: can I use variables when defining a std::array? I'm trying to do this, but i'm getting an error: int user_array; std::cout user_array; std::array arr; Thank you so much!
Great question--because it's a template parameter, we have to know at compile-time the number of items. If 'user_array' for example is constexpr, then you could use a variable, but otherwise the value must be known during compile-time.
Hey Mike .. really good stuff! Interestingly and by accident, I ran the iota snippet without declaring the library for std::begin and std::end. And it compiled and ran successfully (gcc C++17). Could you verify?
@@MikeShah it appears that way. I'm looking a little further at others as well, such as fill method and it appears to have the iterators built in as well with no explicit declaration of as far as I can tell. Learning a lot!
Cheers! This is an inherited behavior from C to have 0-indexed arrays. I believe I was taught that starting from zero gave us the maximum use of integers that we can access an array with 🙂
@@MikeShah Thank you for the reply, so for example, for an array like int arr[5]: arr points to the start of the array arr[0] is at offset 0 (the first element) arr[1] is at offset sizeof(int) from arr arr[2] is at offset 2 * sizeof(int) from arr And so on
I really appreciate the approach you've taken here with introducing STL concepts early on, especially in such a natural way. This often feels like a rushed after thought in other tutorials I've watched.
Thank you for the kind words and feedback! 🙂
Hi Mike! Thanks for this great C++ series!
The only one minor issue I've noticed in this video: I've learned from Jason Turner's videos, that calling std::endl in a loop is considered as a bad practice. He advised to use '
' instead, and if one really wants to flush the buffer so it would be better to use std::flush outside the loop.
Cheers! Yes, that is probably the correct optimization advice for anything I/O bound,
Insightful and conceptual video , great learning ! Thank you.
Cheers!
Very well explained. Thank You.
Cheers, thank you Dhanush!
Hello Mike, for the record, I'm brazilian and I'm really enjoying your lessons!
As for this one, a point raised me a doubt bc when you assigned a value outside the range previously set up to 'Ids' array, you received a 'segmentation fault' message, but in my case I didn't receive anything and it compiled well and even was able to execute. Do you know the reason of this behavior?
If you don't go far enough out of the stack, you may indeed not get a segfault :)
@@MikeShah thanks for the answer Mike!!
Enjoying the series thus far, and just subscribed to your channel
Cheers -- welcome!
Is there a quick way to initialize raw arrays with a default value? Or we always have to use algorithms?
You can always make a raw array 'static' to zero-initialize it, as that memory needs to be reserved. I haven't covered 'constinit' yet, but that can be helpful to gaureentee static initialization. Otherwise, the use of 'constexpr' can shine here for compile-time execution, and filling in your array :) Perhaps this is a separate video in the future.
@@MikeShah tnx❤
Hey Mike. Great video!
Can you please tell us how is the I integer being able to access the elements in the array.
Thank you
Thank you for the kind words! What 'I' integer are your referring to at which timestamp? Happy to help!
Hey Mike, really good video!
Question: can I use variables when defining a std::array?
I'm trying to do this, but i'm getting an error:
int user_array;
std::cout user_array;
std::array arr;
Thank you so much!
Great question--because it's a template parameter, we have to know at compile-time the number of items. If 'user_array' for example is constexpr, then you could use a variable, but otherwise the value must be known during compile-time.
@@MikeShah oh now I get it! Thank you so much! That's why you always specify the "compile-time" thing ;)
@@guitart You got it! :)
Hey Mike .. really good stuff! Interestingly and by accident, I ran the iota snippet without declaring the library for std::begin and std::end. And it compiled and ran successfully (gcc C++17). Could you verify?
Can confirm, perhaps numeric.h includes them?
@@MikeShah it appears that way. I'm looking a little further at others as well, such as fill method and it appears to have the iterators built in as well with no explicit declaration of as far as I can tell. Learning a lot!
👍
thanks for the lesson. however, why we access the first element of an array using 0 and not 1?
Cheers! This is an inherited behavior from C to have 0-indexed arrays. I believe I was taught that starting from zero gave us the maximum use of integers that we can access an array with 🙂
But, probably the right way to think of it is as a "zero offset", meaning in our array we start at the beginning
@@MikeShah Thank you for the reply, so for example, for an array like int arr[5]:
arr points to the start of the array
arr[0] is at offset 0 (the first element)
arr[1] is at offset sizeof(int) from arr
arr[2] is at offset 2 * sizeof(int) from arr
And so on
@@deepblackoutlaw9640 perfect!
How can std::end(ids) infer the size of the raw array when size isn't a property?
std::end knows the type (which is int for 'ids'), and the size at compile-time (In this case, ids is 100) for the static array.