容器适配器之stack

参见http://www.cplusplus.com/reference/stack/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.
   [堆栈(stack)是一种容器适配器,具有后进先出的结构元素的插入和提取都只在容器的一端进行]
   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.
   [标准容器类vector、deque和list都满足这些要求。默认的内在容器是deque]

/*
//construct stack
stack (const container_type& ctnr = container_type());

ctnr
Container object.
[ctnr是一个容器类对象]
container_type is the type of the underlying container type (defined as an alias of the second class template parameter, Container; see member types).
[ctnr的数据类型container_type是内在容器的数据类型,即第二个模板参数Container]

A container adaptor keeps internally a container object as data. This container object is a copy of the ctnr argument passed to the constructor, if any, otherwise it is an empty container.
[stack会将一个容器对象作为数据,这个容器对象是传递到构造函数中的参数ctnr,如果没有传递参数ctnr则为空容器]
*/
#include <iostream>
#include <stack>
#include <vector>
#include <deque>

int main()
{
    std::deque<int> mydeque(3, 100);
    std::vector<int> myvector(2, 200);

    std::stack<int> first;                      // empty stack with deque as underlying container
    std::stack<int> second(mydeque);            // stack initialized to copy of deque with deque as underlying container

    std::stack<int, std::vector<int>> third;              //empty stack with vector as underlying container
    std::stack<int, std::vector<int>> fourth(myvector);   //statck initialized to copy of vector with vector as underlying container

    std::cout << "size of first: " << first.size() << ‘\n‘;
    std::cout << "size of second: " << second.size() << ‘\n‘;
    std::cout << "size of third: " << third.size() << ‘\n‘;
    std::cout << "size of fourth: " << fourth.size() << ‘\n‘;

    system("pause");
    return 0;
}
/*
bool empty() const;
size_type size() const;
void push(const value_type& val);
void pop();
value_type& top();
*/
#include <iostream>
#include <stack>

int main()
{
    std::stack<int> mystack;

    for(int i=0; i<5; i++)
        mystack.push(i*10);

    std::cout<<"mystack containes: ";
    while(!mystack.empty())
    {
        std::cout<<mystack.top()<<‘ ‘;
        mystack.pop();
    }

    std::cout<<‘\n‘;

    system("pause");
    return 0;
}
时间: 2024-08-28 23:59:14

容器适配器之stack的相关文章

容器适配器之priority_queue

template <class T, class Container = vector<T>,                class Compare = less<typename Container::value_type> > class priority_queue; Priority queuePriority queues are a type of container adaptors, specifically designed such that i

容器适配器之queue

转载http://blog.csdn.net/thefutureisour/article/details/7751846容器适配器容器适配器其实就是一个接口转换装置,使得我们能用特定的方法去操作一些我们本来无法操作的东西.举一个例子,比如你的一个设备支持串口线,而你的电脑支持的是USB接口,这时候我们没必要重新买一个支持USB的设备,只需要一根串口转USB口的小玩意儿,让你的设备能够连接到USB接口上,这就是适配器.那么C++中的容器适配器是做什么的呢?可以做一个类比,我们已有的容器(如vec

Android适配器之DataModifyHelper数据操作类的封装

编写适配器代码时常常被以下几个问题所困扰: 1.业务层和适配器中对同一组数据进行维护,难以管理 2.在业务层针对数据进行修改后必须通知适配器更新,否则提示The content of the adapter has changed but ListView did not receive anotification 3.业务层修改数据时充斥大量的非空&数据标准化等冗余代码 针对前两个问题,可以将数据交由适配器去管理,业务层对数据的增删改查均通过适配器进行处理,这样仅需要维护好adapter中的数

函数对象适配器之ptr_fun的使用示例

1 //============================================================================ 2 // Name : CopyInts4.cpp 3 // Author : motein 4 // Version : 5 // Copyright : Your copyright notice 6 // Description : Hello World in C++, Ansi-style 7 //==============

stl容器学习——queue,stack,list与string

目录 string stack queue list 点击上面的内容可以实现跳转哦 简介:本文记录了对string list queue stack四个容器的学习总结,包含有四种容器常用的函数介绍和一些使用过程中碰到的细节总结,在list容器中介绍了迭代器的使用. 头文件 想要用哪一种容器,就要加上对应的头文件 比如想要使用queue , 就要加上 #include<queue> 以此类推,想要使用STL库中的容器,只要加上它们的头文件就好. string 目录部分 1.string的定义及初

c++STL容器之stack容器

栈:先进后出. 一.构造函数 stack<T> stk; stack<const stack& stk>; 二.赋值操作 stack& operator=(const stack &stk); 三.数据存取 push(ele); pop(); top(); 四.大小操作 empty(); size(); 原文地址:https://www.cnblogs.com/xiximayou/p/12111639.html

C++ Primer 学习笔记_45_模板(三):缺省模板参数(借助标准模板容器deque实现Stack模板)、成员模板、关键字typename

一.缺省模板参数 1.stack内存能否借助标准模板容器管理呢?答案是肯定的,只需要多传一个模板参数即可,而且模板参数还可以是缺省的,如下: template <typename T, typename CONT = std::deque<T> > //此处末尾必须有空格,否则编译出错 class Stack { - private: CONT c_; }; 如果没有传第二个参数,默认为deque 双端队列,当然我们也可以传递std::vector<T> 2.示例:借助

C++ Primer 学习笔记_55_STL剖析(十):容器适配器(stack、 queue 、priority_queue)源码浅析与使用示例

七种基本容器:vector.deque.list.set.multiset.map.multimap 一.容器适配器 stack queue priority_queue stack.queue.priority_queue 都不支持任一种迭代器,它们都是容器适配器类型,stack是用vector/deque/list对象创建了一个先进后出容器:queue是用deque或list对象创建了一个先进先出容器:priority_queue是用vector/deque创建了一个排序队列,内部用二叉堆实

Android适配器之定义PagerAdapter万能适配器

一般使用pageAdapter需要实现getcount.isViewFromObject.instantiateItem.destroyItem等默认函数,其实完全可以抽取共有代码到父类当中. /** * Created by Xiaoxuan948 on 2015/10/27. * Desc: * 提示:先给adapter设置数据,再绑定适配器 setAdapter会调用instantiateItem方法 */public class AbsUnityBasePageAdapter<T> e