码迷,mamicode.com
首页 > 其他好文 > 详细

STL源码剖析 容器 stl_queue.h

时间:2014-07-22 00:30:38      阅读:230      评论:0      收藏:0      [点我收藏+]

标签:style   blog   http   color   os   io   

本文为senlie原创,转载请保留此地址:http://blog.csdn.net/zhengsenlie


queue

----------------------------------------------------------------------
stack 是一种配接器(adapter),以某种容器作为底部结构,改变其接口,使之符合"先进先出"的特性。
SGI STL 默认以 deque 为 stack 底部结构
没有遍历行为,没有遍历器


示例:
#include <queue>
#include <list>
#include <iostream>
#include <algorithm>
using namespace std;


int main(){
	queue<int, list<int> >iqueue;
	iqueue.push(1);
	iqueue.push(2);
	cout << iqueue.size() << endl
		 << iqueue.front() << endl;
}

源码:
#ifndef __SGI_STL_INTERNAL_QUEUE_H
#define __SGI_STL_INTERNAL_QUEUE_H


__STL_BEGIN_NAMESPACE


#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = deque<T> >//默认以 deque 为底层容器
#else
template <class T, class Sequence>
#endif
class queue {
  friend bool operator== __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);
  friend bool operator< __STL_NULL_TMPL_ARGS (const queue& x, const queue& y);
public:
  typedef typename Sequence::value_type value_type;
  typedef typename Sequence::size_type size_type;
  typedef typename Sequence::reference reference;
  typedef typename Sequence::const_reference const_reference;
protected:
  Sequence c;//底层容器
public:
  //以下完全利用 Sequence c 的操作完成 queue 的操作
  bool empty() const { return c.empty(); }
  size_type size() const { return c.size(); }
  reference front() { return c.front(); }
  const_reference front() const { return c.front(); }
  reference back() { return c.back(); }
  const_reference back() const { return c.back(); }
  //修改接口使符合 queue "前进先出"的条件
  void push(const value_type& x) { c.push_back(x); }
  void pop() { c.pop_front(); }
};


template <class T, class Sequence>
bool operator==(const queue<T, Sequence>& x, const queue<T, Sequence>& y) {
  return x.c == y.c;
}


template <class T, class Sequence>
bool operator<(const queue<T, Sequence>& x, const queue<T, Sequence>& y) {
  return x.c < y.c;
}

priority_queue
-------------------------------------------------------------------


priority_queue 是一个 queue ,所以只允许在底端加入元素,并从顶端取出元素,
priority_queue 带有权值观念,其内的元素并非依照被推入的次序排列,而是自动依照元素的权值排列。
priority_queue 以底部容器为依据,再加上 heap 处理规则,缺省是以 vector 为底层容器


示例:
int main(){
	int ia[] = {0,1,2,3,4,8,9,3,5};
	priority_queue<int> ipq(ia, ia + 9);
	while(!ipq.empty()){
		cout << ipq.top() << ' ';
		ipq.pop();
	}
	cout << endl;
}
源码:

#ifndef __STL_LIMITED_DEFAULT_TEMPLATES
template <class T, class Sequence = vector<T>, //底部默认采用 vector 容器
          class Compare = less<typename Sequence::value_type> > //默认权值大的排在前面
#else
template <class T, class Sequence, class Compare>
#endif
class  priority_queue {
public:
  typedef typename Sequence::value_type value_type;
  typedef typename Sequence::size_type size_type;
  typedef typename Sequence::reference reference;
  typedef typename Sequence::const_reference const_reference;
protected:
  Sequence c; //底层容器
  Compare comp; //元素大小比较标准
public:
  priority_queue() : c() {}
  explicit priority_queue(const Compare& x) :  c(), comp(x) {}


#ifdef __STL_MEMBER_TEMPLATES
  template <class InputIterator>
  priority_queue(InputIterator first, InputIterator last, const Compare& x)
    : c(first, last), comp(x) { make_heap(c.begin(), c.end(), comp); }
  template <class InputIterator>
  priority_queue(InputIterator first, InputIterator last) 
    : c(first, last) { make_heap(c.begin(), c.end(), comp); }
#else /* __STL_MEMBER_TEMPLATES */
  priority_queue(const value_type* first, const value_type* last, 
                 const Compare& x) : c(first, last), comp(x) {
    make_heap(c.begin(), c.end(), comp);
  }
  priority_queue(const value_type* first, const value_type* last) 
    : c(first, last) { make_heap(c.begin(), c.end(), comp); }
#endif /* __STL_MEMBER_TEMPLATES */


  bool empty() const { return c.empty(); }
  size_type size() const { return c.size(); }
  const_reference top() const { return c.front(); }
  void push(const value_type& x) {
    __STL_TRY {
	  //无利用底层容器的 push_back() 将新元素推入末端,再重排 heap()
      c.push_back(x); 
      push_heap(c.begin(), c.end(), comp);
    }
    __STL_UNWIND(c.clear());
  }
  void pop() {
    __STL_TRY {
	  //将权值最大或最小放在尾端
      pop_heap(c.begin(), c.end(), comp);
	  //将用底层容器的 pop_back() 弹出
      c.pop_back();
    }
    __STL_UNWIND(c.clear());
  }
};


// no equality is provided


__STL_END_NAMESPACE


#endif /* __SGI_STL_INTERNAL_QUEUE_H */


// Local Variables:
// mode:C++
// End:


STL源码剖析 容器 stl_queue.h,布布扣,bubuko.com

STL源码剖析 容器 stl_queue.h

标签:style   blog   http   color   os   io   

原文地址:http://blog.csdn.net/zhengsenlie/article/details/38011277

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!