• C++ std::stack


    std::stack

    template <class T, class Container = deque<T> > class stack;
    

    LIFO stack

    Stacks are a type of container adaptor, specifically designed to operate in a LIFO context (last-in first-out), where elements are inserted and extracted only from one end of the container.

    stacks are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its underlying container, providing a specific set of member functions to access its elements. Elements are pushed/popped from the "back" of the specific container, which is known as the top of the stack.

    The underlying container may be any of the standard container class templates or some other specifically designed container class. The container shall support the following operations:

    • empty
    • size
    • back
    • push_back
    • pop_back

    The standard container classes vector, deque and list fulfill these requirements. By default, if no container class is specified for a particular stack class instantiation, the standard container deque is used.

    Template parameters

    • T Type of the elements. Aliased as member type stack::value_type.
    • Container Type of the internal underlying container object where the elements are stored. Its value_type shall be T. Aliased as member type stack::container_type.
    member type definition notes
    value_type The first template parameter (T) Type of the elements
    container_type The second template parameter (Container) Type of the underlying container
    size_type an unsigned integral type usually the same as size_t

    Member functions

    • (constructor) Construct stack (public member function )
    • empty Test whether container is empty (public member function )
    • size Return size (public member function )
    • top Access next element (public member function )
    • push Insert element (public member function )
    • emplace Construct and insert element (public member function )
    • pop Remove top element (public member function )
    • swap Swap contents (public member function )

    Non-member function overloads

    • relational operators Relational operators for stack (function )
    • swap (stack) Exchange contents of stacks (public member function )

    Non-member class specializations

    • uses__allocator Uses allocator for stack (class template )

    Code Example

    #include <iostream>
    #include <stack>
    #include <vector>
    #include <deque>
    
    using namespace std;
    
    int main(int argc, char **argv)
    {
        deque<int> mydeque(3,10);
        vector<int> myvector(2,20);
    
        stack<int> first;
        stack<int> second(mydeque);
    
        stack<int, vector<int> > third;
        stack<int, vector<int> > fourth(myvector);
    
        /** other function please to see the deque container */
        return 0;
    }
    

    Reference

    cplusplus


  • 相关阅读:
    2008年6月6日今天终于调回公司本部啦,记录历史的一天。
    动易安全开发手册
    今天开机后发现有些图标变了样(图标变灰色),可是功能都能用
    用CFile类简单读写文件
    【转】动态链接库的静态链接导致程序的DLL劫持漏洞借助QQ程序xGraphic32.dll描述
    失败的人只有一种,就是在抵达成功之前放弃的人
    ListControl
    [转贴]仅通过崩溃地址找出源代码的出错行
    tinyxml文档
    得到程序当前UAC的执行权限,高 中 低
  • 原文地址:https://www.cnblogs.com/zi-xing/p/6259721.html
Copyright © 2020-2023  润新知