queue

目录

一、queue

1.1 queue队列

1.2相关函数

 Member functions

Non-member function overloads

Non-member class specializations

二、dequeue

2.1dequeue双端队列

2.2相关函数

Member functions

Non-member functions overloads

三、priority_queue

3.1priority_queue优先级队列//默认less为大堆

3.2相关函数

Member functions

Non-member function overloads

Non-member class specializations


一、queue

1.1 queue队列

//使用deque的封装对象作为queue的底层容器

#include<iostream>
#include<deque>
using namespace std;

namespace bit
{
    template<class T, class Con = deque<T>>
    class queue
    {
    public:
        queue(){}
        void push(const T& x) {
            _c.push_back(x);
        }
        void pop() {
            _c.pop_front();
        }
        T& back() {
           return  _c.back();
        }
        const T& back()const {
            return _c.back();
        }
        T& front() {
            return _c.front();
        }
        const T& front()const {
            return _c.front();
        }
        size_t size()const {
            return _c.size();
        }
        bool empty()const{
            return _c.empty();
        }
    private:
        Con _c;
    };
};

1.2相关函数

 Member functions

(constructor)//可用deque或者list初始化


empty

size

front

back

push

emplace Construct and insert element after its current last element//没有返回值

pop

swap 

Non-member function overloads

relational operators//这些比较运算符要求两个队列的底层容器类型必须相同,并且底层容器中的元素类型也必须支持相应的比较运算。

swap (queue) 

Non-member class specializations

uses_allocator<queue> 


二、dequeue

2.1dequeue双端队列

2.2相关函数

Member functions

(constructor)

Construct deque container (public member function )

(destructor)

Deque destructor (public member function )

operator=

Assign content (public member function )


Iterators:

begin

Return iterator to beginning (public member function )

end

Return iterator to end (public member function )

rbegin

Return reverse iterator to reverse beginning (public member function )

rend

Return reverse iterator to reverse end (public member function )

cbegin 

Return const_iterator to beginning (public member function )

cend 

Return const_iterator to end (public member function )

crbegin 

Return const_reverse_iterator to reverse beginning (public member function )

crend 

Return const_reverse_iterator to reverse end (public member function )


Capacity:

size

Return size (public member function )

max_size

Return maximum size (public member function )

resize//size大于n则删除多的,小于则补充val

Change size (public member function )

empty

Test whether container is empty (public member function )

shrink_to_fit 

Shrink to fit (public member function )


Element access:

operator[]

Access element (public member function )

at

Access element (public member function )

front

Access first element (public member function )

back

Access last element (public member function )


Modifiers:

assign//替换现有的

Assign container content (public member function )

push_back

Add element at the end (public member function )

push_front

Insert element at beginning (public member function )

pop_back

Delete last element (public member function )

pop_front

Delete first element (public member function )

insert

Insert elements (public member function )

erase

Erase elements (public member function )

swap

Swap content (public member function )

clear

Clear content (public member function )

emplace 

Construct and insert element (public member function )

emplace_front //没有返回值

Construct and insert element at beginning (public member function )

emplace_back //没有返回值

Construct and insert element at the end (public member function )


Allocator:

get_allocator

Get allocator (public member function )

 

Non-member functions overloads

relational operators

Relational operators for deque (function )

swap

Exchanges the contents of two deque containers (function template )

三、priority_queue

3.1priority_queue优先级队列//默认less为大堆

//使用vector的封装对象作为priority_queue的底层容器

#include<iostream>
#include<vector>
using namespace std;
namespace bit{
    template <class T, class Container = vector<T>, class Compare = less<T> >
    class priority_queue{
    public:
        priority_queue() {};
        void AdjustDwonwards(T parent) {
            int child = parent * 2 + 1;
            while (child < c.size()) {
                if (child + 1 < c.size() && comp(c[child], c[child + 1])) {
                    child = child + 1;
                }
                if (comp(c[parent], c[child])) {
                    swap(c[child], c[parent]);
                    parent = child;
                    child = parent * 2 + 1;
                }
                else {
                    break;
                }
            }
        }
        //0
        //1 2
        //3 4  5 6
        void AdjustUpwards(T child) {
            int parent = (child - 1) / 2;
            while (child >0) {
                if (child + 1 < c.size() && comp(c[child], c[child + 1])) {
                    child = child + 1;
                }
                if (comp(c[parent], c[child])) {
                    swap(c[child], c[parent]);
                    child = parent;
                    parent = (child - 1) / 2;
                }
                else {
                    break;
                }
            }
        }

        template <class InputIterator>
        priority_queue(InputIterator first, InputIterator last) {
            for (InputIterator it = first; it != last;) {
                c.push_back(*it);
                it++;
            }
            for (int i = c.size() / 2 - 1; i >= 0; i--) {
                AdjustDwonwards(i);
            }
        }
        bool empty() const {
            return c.empty();
        }
        size_t size() const {
            return c.size();
        }
        const T& top() const {
            return c[0];
        }
        void push(const T& x) {
            c.push_back(x);
            AdjustUpwards(c.size() - 1);
        }
        void pop() {
            swap(c[0], c[c.size() - 1]);
            c.pop_back();
            AdjustDwonwards(0);
        }
    private:
        Container c;
        Compare comp;
    };
};

3.2相关函数

Member functions

(constructor)

Construct priority queue (public member function )

empty

Test whether container is empty (public member function )

size

Return size (public member function )

top

Access top 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

swap (queue) 

Exchange contents of priority queues (public member function )

 

Non-member class specializations

uses_allocator<queue> 

Uses allocator for priority queue (class template )

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值