C++编程(F)

141 mapstringintstringcmp m3 17141 mapstringint

Info iconThis preview shows page 1. Sign up to view the full content.

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: } bool empty() const { return c.empty(); } size_type size() const { return c.size(); } value_type& top() { return c.back(); } const value_type& top() const { return c.back(); } void push(const value_type& x) { c.push_back(x); } void pop() { c.pop_back(); } }; Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 堆栈-stack stack就是某容器的一个简单界面,容器的类型作 为模板参数传递给stack。stack所做的全部事情就 是从它的容器的界面中删除所有非stack操作,并 将back()、push_back()和pop_back()改为人 们所习惯的名字top()、push()和pop() top()函数只读取、pop()操作只删除栈顶元素 按默认规定,stack用一个deque来保存自己的元 素,但也可以采用任何提供了back()、 push_back()和pop_back()的序列 stack<char> s1; // 用deque<char>保存char类型的元 stack<int, vector<int>> s2; // 用vector<int>保存int类型的元素 Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 17.3.2 队列-queue queue在<queue>里定义,它也是一个容器的界面,该容器 应该允许在back()处插入新元素,且能从front()提取出来 template <class T, class C = deque<T> > class std::queue { protected: C c; public: typedef typename C::value_type value_type; typedef typename C::size_type size_type; typedef C container_type; explicit queue(const C& a =C()) : c(a) { } bool empty() const { return c.empty(); } size_type size() const { return c.size(); } value_type& front() { return c.front(); } const value_type& front() const { return c.front(); } value_type& back() { return c.back(); } const value_type& back() const { return c.back(); } void push(const value_ type& x) { c.push_back(x); } void pop() { c.pop_front(); } }; Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 队列-queue 按照默认规定,queue用deque保存自己的 元素,但是任何提供了front()、back()、 push_back()和pop_front()的序列都可以 用(vector没有提供pop_front(),所以不 能作为queue的基础容器) 对于queue,也是使用push()添加元素, 使用pop()删除元素 Beijing University of Posts & Telecommunications Beijing young@buptnet.edu.cn 17.3.3 优先队列-priority_queue template <class T, class C = vector<T>, class Cmp = less<typename C::value_type>> class std::priority_queue{ protected: C c; Cmp cmp; public: typedef typename C::value_type value_type; typedef typename C::size_type size_type; typedef C container_type; explicit priority_queue(const Cmp& a1 =Cmp() , const C& a2 =C()) : c(a2) , cmp(a1) { } template <class In> priority_queue(In first, In last, const Cmp& =Cmp() , const C& =C()); bool empty() const { return c.empty(); } size_type...
View Full Document

This note was uploaded on 02/10/2014 for the course ECE C++ taught by Professor Xiaoshengtang during the Fall '13 term at BUPT.

Ask a homework question - tutors are online