C++ STL std::stack (a container adaptor) | Modern Cpp Series Ep. 131

Поділитися
Вставка
  • Опубліковано 29 вер 2024
  • ►Full C++ Series Playlist: • The C++ Programming La...
    ►Find full courses on: courses.mshah.io/
    ►Join as Member to Support the channel: / @mikeshah
    ►Lesson Description: In this lesson I will show you a fundamental data structure -- the stack! Stacks have two key operations, push and pop in computer science. The STL organizes stack such that each operation does exactly one thing, and push, pop, top, and empty for example are as they sound. I'll also describe what it means to be a container adaptor, and that the stack data structure itself enforces a policy, while the underlying data structure has impacts on performance. Finally, I'll show you a use case of stack with an undo and redo system.
    ►UA-cam Channel: / mikeshah
    ►Please like and subscribe to help the channel!
    ►Join our free community: courses.mshah....

КОМЕНТАРІ • 14

  • @qcnck2776
    @qcnck2776 Рік тому +4

    Looking forward to the video on emplace. I've never been sure as to when one should use it. Thanks.
    And once again, appreciate the real world example of stack usage.

  • @nandhan6637
    @nandhan6637 Рік тому +5

    thanks for explaining with analogy and for this useful video...

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

    Do one thing, but do it right (top vs pop) ;)

  • @mytech6779
    @mytech6779 9 місяців тому

    I'm pretty sure most browser histories are implemented using linked lists rather than copying between twin stacks.
    Stacks are useful for recursive-like behavior or where order doesn't really matter, and when everything written to it should only be accessed once. (And yes coming up with practical examples is difficult.)

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

      I would not be surprised at all browsers used a list -- even a simple array (or a circular queue) would do the trick.

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

    Thank you again

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

    @21:41 If we're only adding or removing elements from the end of the container, std::vector would be an efficient choice for the underlying container of a std::stack, right?
    std::vector is typically more space efficient than std::list due to the way it stores its elements. by the way awesome video! my mind has blown away by how much this amazing C++ language is powerful and flexible where we can have user-define underlying data structure/container for adaptive containers! That's what i LOVE about this language : you have power as a programmer and you can see and simulate what is happening behind implementation in C++! Thanks for this great channel! You're awesome!
    💚💚

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

      Cheers! You're correct on vector, but I think it's more so when the allocations are occurring (i.e. all at once, or perhaps over time where a deque may be interesting to profile against).

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

      @@MikeShah I see. That makes sense. Thanks for the response,

    • @mytech6779
      @mytech6779 9 місяців тому

      When a vector allocates more space it creates a whole new vector of twice the capacity and copies over all of the old data (normally it is double, but could be 1.5× or some other amount), a deque just allocates a new empty block and adds a pointer to it (like a doubly-linked list of arrays)