How C++ Vector Works

Sdílet
Vložit
  • čas přidán 11. 09. 2024

Komentáře • 57

  • @abbasio
    @abbasio Před 8 měsíci +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.

  • @Dannnneh
    @Dannnneh Před rokem +5

    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 :/

  • @ayoubelmhamdi7920
    @ayoubelmhamdi7920 Před rokem +7

    this is one of best c/cpp vectors tutorial

  • @masabh
    @masabh Před rokem +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.

  • @achyuththouta6957
    @achyuththouta6957 Před rokem +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++

  • @fabianrr
    @fabianrr Před rokem +6

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

  • @phlimma
    @phlimma Před rokem +3

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

    • @pikuma
      @pikuma  Před rokem +1

      Welcome aboard! Abraços!!! :)

    • @phlimma
      @phlimma Před rokem

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

  • @Luard-xq3zd
    @Luard-xq3zd Před 6 měsíci

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

  • @dudu88games
    @dudu88games Před rokem +4

    Ótima explicação, parabéns!

  • @AQFearfullMage
    @AQFearfullMage Před 5 měsíci

    Thank you, this is very useful.

  • @gammyhorse
    @gammyhorse Před rokem

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

  • @baixodedata2802
    @baixodedata2802 Před rokem +3

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

    • @pikuma
      @pikuma  Před rokem +1

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

  • @arturmg2068
    @arturmg2068 Před rokem

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

  • @TheJGAdams
    @TheJGAdams Před rokem +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  Před rokem +2

      There are many homebrew games developed in C.

  • @dmitrysavkin5981
    @dmitrysavkin5981 Před 5 měsíci

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

  • @Miura-Anjin
    @Miura-Anjin Před rokem +1

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

  • @nickvatanshenas9251
    @nickvatanshenas9251 Před 8 měsíci

    Great video!

  • @ticiusarakan
    @ticiusarakan Před 11 měsíci

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

  • @DevSlowz
    @DevSlowz Před rokem +2

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

  • @SachinHiremath-kw2rw
    @SachinHiremath-kw2rw Před rokem +1

    Nice explaination

  • @starc0w
    @starc0w Před rokem

    Great! Thank you Gustavo!

  • @starc0w
    @starc0w Před rokem +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  Před rokem +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 Před rokem

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

  • @preoalex8298
    @preoalex8298 Před rokem

    What font are you using in your IDE

  • @uanbu6539
    @uanbu6539 Před rokem

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

  • @desossandoTI
    @desossandoTI Před rokem

    Very good !!! thanks!

  • @DanielTolentino42
    @DanielTolentino42 Před 10 měsíci

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

  • @danilonascimento3545
    @danilonascimento3545 Před rokem +1

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

    • @pikuma
      @pikuma  Před rokem +1

      Grande Danilo! Obrigado.

  • @katrielaraujo5090
    @katrielaraujo5090 Před rokem

    Seu canal parece muito legal o Akita indicou e estou gostando

  • @pablouesc
    @pablouesc Před rokem +1

    Vim pelo Akita! Já inscrito no canal!

    • @pikuma
      @pikuma  Před rokem

      Grande Pablo! Abraços.

  • @wakeupneo101
    @wakeupneo101 Před rokem

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

    • @pikuma
      @pikuma  Před rokem

      Opa James, obrigado! Um grande quebra costela. 🙂

  • @poopingnuts
    @poopingnuts Před 9 měsíci

    Eu reconheço o sotaque brasileiro quando eu escuto

    • @pikuma
      @pikuma  Před 9 měsíci

      Me descobriram! 😅

  • @user-cw9po9ty7v
    @user-cw9po9ty7v Před rokem

    what font do you use?

  • @mateivoican
    @mateivoican Před 7 měsíci +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++;
    }

  • @abrorabyyu6221
    @abrorabyyu6221 Před rokem

    this person is cool af

  • @lucassamuel6069
    @lucassamuel6069 Před rokem +1

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

    • @pikuma
      @pikuma  Před rokem +1

      yes

    • @lucassamuel6069
      @lucassamuel6069 Před rokem +1

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

    • @pikuma
      @pikuma  Před rokem

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

    • @leandrocastro1291
      @leandrocastro1291 Před rokem +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.

  • @antoinedevldn
    @antoinedevldn Před rokem

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

  • @Felipekimst
    @Felipekimst Před rokem

    chimarrão

    • @pikuma
      @pikuma  Před rokem

      Claaaaro! ❤️🙂🇧🇷

  • @andreliciosantos6653
    @andreliciosantos6653 Před rokem +2

    Devia criar conteúdo em português em 👀 ( se já tiver ignora kkk) vim pelo Akita

  • @therealherbzy
    @therealherbzy Před rokem +1

    Great video!