Understanding C++ Vector (Dynamic Arrays)

Поделиться
HTML-код
  • Опубликовано: 4 ноя 2024

Комментарии • 59

  • @abbasio
    @abbasio 10 месяцев назад +3

    Thanks for this video! I'm currently taking your C++ 2D Game Engine course, and took this detour to better understand the Vector class, since I'm coming from a web development background where everything is much more abstracted. I made an insert implementation as follow:
    void Insert(int index, T value){
    //Handle the case where we're already at the max size before inserting
    if(size == capacity){
    T* newArray = new T[capacity * 2];
    for (int i = 0; i < size; i++){
    newArray[i] = elements[i];
    }
    delete[] elements;
    elements = newArray;
    capacity *= 2;
    }
    //Increase the size by 1
    size++;
    //Loop over the array in reverse, setting each value equal to the one before, stopping at the given index
    for(int i = size - 1; i >= index; i--){
    elements[i + 1] = elements[i];
    }
    //Replace the given index with the given value
    elements[index] = value;
    }
    It's basic and there might be some edge cases I overlooked, but it seems to work from my testing.

  • @ayoubelmhamdi7920
    @ayoubelmhamdi7920 2 года назад +7

    this is one of best c/cpp vectors tutorial

  • @phlimma
    @phlimma Год назад +3

    One more follower. I came through @Akitando channel. Very nice explanation.

    • @pikuma
      @pikuma  Год назад +1

      Welcome aboard! Abraços!!! :)

    • @phlimma
      @phlimma Год назад

      @@pikuma and please accept Akita's defy to choose 3 linux distros. ;-)

  • @achyuththouta6957
    @achyuththouta6957 Год назад +3

    Wow your knowledge about this is so deep. I'm trying to develop my skill and knowledge in programming as I'm a beginner in it. I've started C++ because some people said it's the hardest to learn so I thought why not? I like learning hard stuff but theres way too much in C++

  • @Luard-xq3zd
    @Luard-xq3zd 8 месяцев назад

    I recently started learning about vectors and I found this vid really helpful

  • @fabianrr
    @fabianrr 2 года назад +6

    Eso es lo que le falta a tu canal de RUclips, algo desde lo básico y de ahí pasar a tus cursos

  • @masabh
    @masabh Год назад +2

    I really should've found your youtube channel earlier. I'm doing your assembly language course on udemy too. You are a very good teacher.

  • @arturmg2068
    @arturmg2068 Год назад

    i cant belive your channel just have 10k subscribers, your videos are so good!!

  • @TheJGAdams
    @TheJGAdams 2 года назад +2

    I love how you kept the memories of the classic retro games alive.
    Good stuff! :)
    So, vector and C++ can work for console like NES? If so, that's really cool!

    • @pikuma
      @pikuma  2 года назад +2

      There are many homebrew games developed in C.

  • @gammyhorse
    @gammyhorse Год назад

    Every single one of your videos is awesome. Thank you!

  • @baixodedata2802
    @baixodedata2802 Год назад +3

    tu é lindão em ..... vim pelo akita

    • @pikuma
      @pikuma  Год назад +1

      Grande Akita! :) Abração e obrigado pelo comment.

  • @Miura-Anjin
    @Miura-Anjin Год назад +1

    Thanks a lot. It makes sense now. What do you suggest me if I decide to my own implement of stack and queue?

  • @dudu88games
    @dudu88games Год назад +4

    Ótima explicação, parabéns!

  • @DevSlowz
    @DevSlowz 2 года назад +2

    Great content as always. Keep up the great work... ps your paid courses are amazing!

  • @starc0w
    @starc0w Год назад +2

    Dear Professor Pezzi
    Thanks for the video! Very well explained!
    If I wanted to do something like this in C. What do I have to pay attention to, besides checking whether there is enough memory (malloc). Are there things that I absolutely have to do? (You talked about the whole thing not being so trivial).

    • @pikuma
      @pikuma  Год назад +1

      Good question!
      We have to use a similar idea, but allocate using malloc(), reallocating/doubling size when necessary using realloc(), and freeing resources with free().
      To achieve some sort of "generic" type handling with your dynamic array (for example, being able to create and manipulate different types), we could take advantage of void* and use macros to resolve the type dynamically at compile time.
      Thse repos are good examples of a simple dynamic array implementation in C99 using the ideas I mentioned above.
      github.com/eignnx/dynarray
      github.com/gustavopezzi/dynamicarray

    • @starc0w
      @starc0w Год назад

      @@pikuma Very good, thanks for the advice and sources. I'll be happy to look at that!

  • @pablouesc
    @pablouesc Год назад +1

    Vim pelo Akita! Já inscrito no canal!

    • @pikuma
      @pikuma  Год назад

      Grande Pablo! Abraços.

  • @SachinHiremath-kw2rw
    @SachinHiremath-kw2rw Год назад +1

    Nice explaination

  • @Dannnneh
    @Dannnneh Год назад +8

    The title made me think this would be an in-depth look at the machinations of how the standard library's vector _really_ works, but this is a very basic introduction. Your element-removals do not even call any potential destructor :/

  • @AQFearfullMage
    @AQFearfullMage 6 месяцев назад

    Thank you, this is very useful.

  • @DanielTolentino42
    @DanielTolentino42 Год назад

    Awsome! Do you have the dotfiles of your vi setup that you can share? Thanks

  • @uanbu6539
    @uanbu6539 Год назад

    Hi Gustavo, do you have a video about socket programming using c++?

  • @jackgame8841
    @jackgame8841 Месяц назад

    37:28 in lines 23 why it had a lot of const? first const and last const?

  • @nickvatanshenas9251
    @nickvatanshenas9251 10 месяцев назад

    Great video!

  • @starc0w
    @starc0w Год назад

    Great! Thank you Gustavo!

  • @dmitrysavkin5981
    @dmitrysavkin5981 7 месяцев назад

    What about, if T doesn't have constructor? new T[2 * n] throws errror

  • @danilonascimento3545
    @danilonascimento3545 Год назад +1

    vim pelo akita, gostei muito do canal parabéns.

    • @pikuma
      @pikuma  Год назад +1

      Grande Danilo! Obrigado.

  • @ticiusarakan
    @ticiusarakan Год назад

    а зачем float? например 320х240 разрешение предполагает что нам не нужны знаки после запятой. или я что то недопонял?

  • @katrielaraujo5090
    @katrielaraujo5090 Год назад

    Seu canal parece muito legal o Akita indicou e estou gostando

  • @wakeupneo101
    @wakeupneo101 Год назад

    Parabéns xirú. Não conhecia teu canal, acabei chegando aqui pelo Akita. Muito bom. Inscrito e fortalecendo a comunidade.

    • @pikuma
      @pikuma  Год назад

      Opa James, obrigado! Um grande quebra costela. 🙂

  • @preoalex8298
    @preoalex8298 Год назад

    What font are you using in your IDE

  • @desossandoTI
    @desossandoTI Год назад

    Very good !!! thanks!

  • @abrorabyyu6221
    @abrorabyyu6221 Год назад

    this person is cool af

  • @mateivoican
    @mateivoican 9 месяцев назад +1

    void Insert(int index, T value){
    if (size == capacity){
    T* newarr = new T[capacity*2];
    for (int i = 0; i index; i--)
    {
    elements[i] = elements[i-1];
    }
    elements[index] = value;
    size++;
    }

  • @AurelianBuia
    @AurelianBuia Год назад

    what font do you use?

  • @antoinedevldn
    @antoinedevldn Год назад

    The internet needs to know what you are drinking in this crazy glass!

  • @lucassamuel6069
    @lucassamuel6069 Год назад +1

    I'm sure your first language is portuguese, are you brazilian?

    • @pikuma
      @pikuma  Год назад +1

      yes

    • @lucassamuel6069
      @lucassamuel6069 Год назад +1

      @@pikuma Cool, I know you didn't ask, but I'm studying Computer Engineering at UTFPR in Paraná-BR

    • @pikuma
      @pikuma  Год назад

      @@lucassamuel6069 Haha. Que legal. Eu tenho vários amigos por lá. Sou do RS. 🙂

    • @leandrocastro1291
      @leandrocastro1291 Год назад +1

      Gustavo, tenho uns cursos seus na Udemy e fiquei desconfiado quando vi um chimarrao em uma das aulas haha.
      Vc é fera, ensina muito bem.

  • @dghung2854
    @dghung2854 Месяц назад +1

    bro use typewriter for coding 💀

  • @Felipekimst
    @Felipekimst Год назад

    chimarrão

    • @pikuma
      @pikuma  Год назад

      Claaaaro! ❤️🙂🇧🇷

  • @therealherbzy
    @therealherbzy 2 года назад +1

    Great video!