自学内容网 自学内容网

【C++】适配器stack/queue/priority_queue使用和实现

目录

容器适配器 

什么是容器适配器 

​编辑stack

stack的了解和使用 

使用举例 

题目加深 

模拟实现 

功能实现 

测试文件 

​编辑queue 

queue的了解和使用 

 使用举例

 题目加深

 模拟实现

功能实现

 测试文件

 priority_queue

priority_queue的了解和使用 

使用举例

题目加深

模拟实现 

 功能实现

 测试文件


容器适配器 

什么是容器适配器 

适配器是一种设计模式(设计模式是一套被反复使用的、多数人知晓的、经过分类编目的、代码设计经验的总结),该种模式是将一个类的接口转换成客户希望的另外一个接口。

stack

stack的了解和使用 

 

函数说明接口说明
stack()构造栈
empty()检测stack是否为空
size()返回stack中有效元素的个数
top()返回栈顶元素的引用
push()将元素val压入stack中
pop()将元素弹出stack中

使用举例 

int main()
{
// 默认展开std
// stack
// 构造方面
stack<int> st;  // 空栈
st.push(1);  // 栈顶插入
st.push(2); 
st.push(3); 
st.push(4); 
st.push(5);
while (!st.empty())
{
cout << st.top() << " ";
cout << "size:" << st.size() << endl;
st.pop();
}

return 0;
}

题目加深 

155. 最小栈 - 力扣(LeetCode)

class MinStack {
public:
    MinStack() {

    }
    
    void push(int val) {
        // 如果最小栈为空或者val元素小于等于最小栈的栈顶元素,则插入最小栈
        // 注意:等于时也要插入,防止正常栈删除时最小栈的元素未删除的情况
        if(_minst.size() == 0 || val <= _minst.top())
        {
            _minst.push(val);
        }
        _st.push(val);
    }
    
    void pop() {
        // 如果栈顶元素相同都要删除
        if(_st.top() == _minst.top())
        {
            _minst.pop();
        }
        _st.pop();
    }
    
    int top() {
        return _st.top();
    }
    
    int getMin() {
        return _minst.top();
    }
private:
    stack<int> _st;     // 正常栈
    stack<int> _minst;  // 最小栈
};

栈的压入、弹出序列_牛客题霸_牛客网 (nowcoder.com)

class Solution {
public:
    /**
     * 代码中的类名、方法名、参数名已经指定,请勿修改,直接返回方法规定的值即可
     *
     * 
     * @param pushV int整型vector 
     * @param popV int整型vector 
     * @return bool布尔型
     */
    bool IsPopOrder(vector<int>& pushV, vector<int>& popV) {
        // write code here
        stack<int> st;
        int i = 0;
        for(auto& e : pushV)
        {
            st.push(e);
            while(!st.empty() && st.top() == popV[i])
            {
                st.pop();
                ++i;
            }
        }
        return st.empty();
    }
};

 150. 逆波兰表达式求值 - 力扣(LeetCode)

class Solution {
public:
    int evalRPN(vector<string>& tokens) {
        stack<int> st;
        for(size_t i = 0; i < tokens.size(); ++i)
        {
            string& in = tokens[i];
            if(!(in == "+" || in == "-" || in == "*" || in == "/"))
            {
                st.push(atoi(in.c_str()));
            }
            else{
                int right = st.top();
                st.pop();
                int left = st.top();
                st.pop();
                switch(in[0])
                {
                case '+':
                    st.push(left + right);
                    break;
                case '-':
                    st.push(left - right);
                    break;
                case '*':
                    st.push(left * right);
                    break;
                case '/':
                    st.push(left / right);
                    break;                   
                }             
            }
        }
        return st.top();
    }
};

模拟实现 

功能实现 

template<class T>
class Stack
{
public:
void push(const T& val) { _v.push_back(val); }
void pop() { _v.pop_back(); }
size_t size(){ return _v.size(); }
bool empty() { return _v.empty(); }
const T& top() const { return _v.back(); }
T& top() { return _v.back(); }
private:
vector<T> _v;
};

测试文件 

int main()
{
ouyang::Stack<int> st;
st.push(1);  
st.push(2);
st.push(3);
st.push(4);
st.push(5);
while (!st.empty())
{
cout << st.top() << " ";
cout << "size:" << st.size() << endl;
st.pop();
}
return 0;
}

queue 

queue的了解和使用 

函数声明接口说明
queue()构造空队列
empty()检测队列是否为空
size()返回队列中有效元素个数
front()返回队头元素的引用
back()返回队尾元素的引用
push()在队尾将元素val插入
pop()将队头元素出队列

 使用举例

int main()
{
queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
while (!q.empty())
{
cout << "front" << q.front() << " ";
cout << "back" << q.back() << " ";
cout << "size" << q.size() << " ";
q.pop();
cout << endl;
}
return 0;
}

 题目加深

225. 用队列实现栈 - 力扣(LeetCode)

class MyStack {
public:
    MyStack() {}

    void push(int x) { 
        _q1.push(x); 
        }

    int pop() {
        int num = 0;
        if (!_q1.empty()) {
            while (_q1.size() > 1) {
                _q2.push(_q1.front());
                _q1.pop();
            }
            num = _q1.front();
            _q1.pop();
        }
        else{
            while (_q2.size() > 1) {
                _q1.push(_q2.front());
                _q2.pop();
            }
            num = _q2.front();
            _q2.pop();
        }
        return num;
    }

    int top() {
        if (!_q1.empty()) {
            return _q1.back();
        } else {
            return _q2.back();
        }
    }

    bool empty() { 
        return _q1.empty() && _q2.empty(); 
        }

private:
    queue<int> _q1;
    queue<int> _q2;
};

 模拟实现

功能实现

template<class T>
class Queue
{
public:
void push(const T& val) { _ls.push_back(val); }
void pop() { _ls.pop_front(); }
size_t size() const { return _ls.size(); }
bool empty() const { return _ls.empty(); }
T& front() { return _ls.front(); }
const T& front() const { return _ls.front(); }
T& back() { return _ls.back(); }
const T& back() const { return _ls.back(); }
private:
list<T> _ls;
};

 测试文件

int main()
{
ouyang::Queue<int> q;
q.push(1);
q.push(2);
q.push(3);
q.push(4);
q.push(5);
while (!q.empty())
{
cout << "front" << q.front() << " ";
cout << "back" << q.back() << " ";
cout << "size" << q.size() << " ";
q.pop();
cout << endl;
}
return 0;
}

 priority_queue

priority_queue的了解和使用 

函数声明接口说明
priority_queue() / priority_queue(first, last)

构造一个空的优先级队列(堆)

使用迭代器构造一个优先级队列(堆)

empty()检测优先级队列(堆)是否为空
top()返回优先级队列中最大(最小)的元素引用,即堆顶元素
push(x)在优先级队列(堆)中插入元素x
pop()删除优先级队列中最大(最小)的元素引用,即堆顶元素

使用举例

int main()
{
// 默认情况下,创建的是大堆,其底层按照小于号比较
vector<int> v{ 3,2,7,6,0,4,1,9,8,5 };
priority_queue<int> q1;
for (auto& e : v)
q1.push(e);
cout << q1.top() << endl;
// 如果要创建小堆,将第三个模板参数换成greater比较方式
priority_queue<int, vector<int>, greater<int>> q2(v.begin(), v.end());
cout << q2.top() << endl;
return 0;
}

题目加深

215. 数组中的第K个最大元素 - 力扣(LeetCode)

class Solution {
public:
    int findKthLargest(vector<int>& nums, int k) {
        priority_queue<int> pq(nums.begin(), nums.end());
        for(int i = 0; i < k-1; ++i)
        {
            pq.pop();
        }
        return pq.top();
    }
};

模拟实现 

 功能实现

template<class T>
struct less
{
bool operator()(const T& left, const T& right)
{
return left < right;
}
};

template<class T>
struct greater
{
bool operator()(const T& left, const T& right)
{
return left > right;
}
};

template<class T, class Container = std::vector<T>, class Compare = less<T>>
class Priority_queue
{
public:
Priority_queue() :_c() {};

template<class Iterator>
Priority_queue(Iterator first, Iterator last)
:_c(first, last)
{
int root = (_c.size() - 2) >> 1;
for (; root >= 0; --root)
{
AdjustDown(root);
}
}

void push(const T& val)
{
_c.push_back(val);
AdjustUp(_c.size() - 1);
}

void pop()
{
if (empty())
return;
swap(_c.front(), _c.back());
_c.pop_back();
AdjustDown(0);
}

size_t size() const { return _c.size(); }
bool empty() const { return _c.empty(); }

const T& top() const { return _c.front(); }

private:
void AdjustUp(int child)
{
int parent = (child - 1) >> 1;
while (child)
{
if (Compare()(_c[parent], _c[child]))
{
swap(_c[parent], _c[child]);
child = parent;
parent = (child - 1) >> 1;
}
else {
break;
}
}
}

void AdjustDown(int parent)
{
int child = parent * 2 + 1;
while (child < _c.size())
{
if (child + 1 < _c.size() && Compare()(_c[child], _c[child + 1]))
{
child += 1;
}
if (Compare()(_c[parent], _c[child]))
{
swap(_c[parent], _c[child]);
parent = child;
child = parent * 2 + 1;
}
else {
break;
}
}
}

Container _c;
};

 测试文件

int main()
{
// 默认情况下,创建的是大堆,其底层按照小于号比较
vector<int> v{ 3,2,7,6,0,4,1,9,8,5 };
ouyang::Priority_queue<int> q1;
for (auto& e : v)
q1.push(e);
cout << q1.top() << endl;

 如果要创建小堆,将第三个模板参数换成greater比较方式
ouyang::Priority_queue<int, vector<int>, greater<int>> q2(v.begin(), v.end());
cout << q2.top() << endl;
return 0;
}


原文地址:https://blog.csdn.net/2301_81577798/article/details/142785381

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!