Raw Arrays and std::array in C++ | Modern Cpp Series Ep. 13

Поділитися
Вставка
  • Опубліковано 2 лис 2024

КОМЕНТАРІ • 33

  • @scotto-robotto
    @scotto-robotto Рік тому +7

    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.

    • @MikeShah
      @MikeShah  Рік тому

      Thank you for the kind words and feedback! 🙂

  • @dino_source
    @dino_source Рік тому +3

    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.

    • @MikeShah
      @MikeShah  Рік тому

      Cheers! Yes, that is probably the correct optimization advice for anything I/O bound,

  • @shreya_si9gh
    @shreya_si9gh Місяць тому +1

    Insightful and conceptual video , great learning ! Thank you.

  • @dhanushs1802
    @dhanushs1802 2 роки тому +6

    Very well explained. Thank You.

    • @MikeShah
      @MikeShah  2 роки тому

      Cheers, thank you Dhanush!

  • @matteusgutemberg5603
    @matteusgutemberg5603 Місяць тому +1

    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?

    • @MikeShah
      @MikeShah  Місяць тому +1

      If you don't go far enough out of the stack, you may indeed not get a segfault :)

    • @matteusgutemberg5603
      @matteusgutemberg5603 Місяць тому +1

      @@MikeShah thanks for the answer Mike!!

  • @chesspool11
    @chesspool11 Рік тому +1

    Enjoying the series thus far, and just subscribed to your channel

  • @farianderson168
    @farianderson168 3 місяці тому +1

    Is there a quick way to initialize raw arrays with a default value? Or we always have to use algorithms?

    • @MikeShah
      @MikeShah  3 місяці тому

      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.

    • @farianderson168
      @farianderson168 3 місяці тому

      @@MikeShah tnx❤

  • @kaydend5192
    @kaydend5192 2 роки тому +2

    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

    • @MikeShah
      @MikeShah  2 роки тому

      Thank you for the kind words! What 'I' integer are your referring to at which timestamp? Happy to help!

  • @guitart
    @guitart 2 роки тому +2

    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!

    • @MikeShah
      @MikeShah  2 роки тому +1

      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.

    • @guitart
      @guitart 2 роки тому +1

      @@MikeShah oh now I get it! Thank you so much! That's why you always specify the "compile-time" thing ;)

    • @MikeShah
      @MikeShah  2 роки тому +1

      @@guitart You got it! :)

  • @GaryChike
    @GaryChike 2 роки тому +1

    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
      @MikeShah  2 роки тому +1

      Can confirm, perhaps numeric.h includes them?

    • @GaryChike
      @GaryChike 2 роки тому +1

      @@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!

  • @guilherme5094
    @guilherme5094 3 місяці тому +1

    👍

  • @deepblackoutlaw9640
    @deepblackoutlaw9640 10 місяців тому

    thanks for the lesson. however, why we access the first element of an array using 0 and not 1?

    • @MikeShah
      @MikeShah  10 місяців тому

      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
      @MikeShah  10 місяців тому

      But, probably the right way to think of it is as a "zero offset", meaning in our array we start at the beginning

    • @deepblackoutlaw9640
      @deepblackoutlaw9640 10 місяців тому

      @@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

    • @MikeShah
      @MikeShah  10 місяців тому

      @@deepblackoutlaw9640 perfect!

  • @swagatochatterjee7104
    @swagatochatterjee7104 10 місяців тому +1

    How can std::end(ids) infer the size of the raw array when size isn't a property?

    • @MikeShah
      @MikeShah  10 місяців тому

      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.