stack_queue的详细解释和使用

stack是栈,没有迭代器,以下是相关代码测试;

	//stack容器适配器 没有迭代器 不能用auto top:栈顶  pop:出栈
namespace ghc
{
	template<class _Ty>
	class stack
	{
	public:
		stack()//list自己会做
		{}
		~stack()//list自己会做
		{}
	public:
		bool empty()const
		{
			return _C.empty();
		}
		size_t size()const
		{
			return _C.size();
		}
		_Ty& top()
		{
			return _C.back();
		}
		void push(const _Ty &x)
		{
			_C.push_back(x);
		}
		void pop()
		{
			_C.pop_back();
		}
	private:
		list<_Ty> _C;  //容器适配器 用list作为stack的基础  vector也可以做到
	};
}

namespace ghc
{
	template<class _Ty>
	class stack
	{
	public:
		stack()
		{
			capacity = STACK_DEFAULT_SIZE;
			tp = 0;
			base = new _Ty[capacity];
		}
		~stack()
		{
			delete[]base;
			bast = nullptr;
			capacity = 0;
			tp = 0;
		}
	public:
		bool empty()const
		{
			return tp == 0;
		}
		size_t size()const
		{
			return tp;
		}
		_Ty& top()
		{
			assert(tp != 0);
			return base[tp - 1];
		}
		void push(const _Ty &x)
		{
			if (tp >= capacity)
			{
				//扩容
			}
			base[tp++] = x;
		}
		void pop()
		{
			assert(tp != 0);
			tp--;
		}
	private:
		enum{STACK_DEFAULT_SIZE=8};
		_Ty* base;
		size_t capacity;
		size_t tp;
	};
}
void main()
{
	ghc::stack<int> st;
}

class MinStack     //最小栈
{
public:
	MinStack()
	{}
	void push(int val)
	{
		data_st.push(val);
		if (min_st.empty() || val <= min_st.top())
			min_st.push(val);
	}
	void pop()
	{
		int val = data_st.top();
		data_st.pop();
		if (val == min_st.top())
			min_st.pop();
	}
	int top()
	{
		return data_st.top();
	}
	int getmin()
	{
		return min_st.top();
	}
private:
	stack<int> data_st;
	stack<int> min_st;

};
void main()
{
	stack<int> st;
	for (int i = 0; i < 10; ++i)
		st.push(i);//push入栈
	cout << st.size() << endl;
	while (!st.empty())
	{
		int value = st.top();//栈顶值付给value
		st.pop();//栈顶出栈,出去就没了
		cout << value << endl;
	}
}

queue,队列,push入队,pop出队,对于访问要求比较高用队列,对于头插尾插频繁的用双端队列deque
eg

void main()
{
	deque<int> dq;
	dq.push_back(1);
	dq.push_back(2);
	dq.push_back(3);
	dq.push_front(10);
	dq.push_front(20);
	dq.push_front(30);
}

stack queue 操作受限制的数据结构,只能在尾部插入或者删除。
queue和stack方向相反
双端队列并不是真正的连续空间,而是由一段连续的小空间拼接而成。

template<class _Ty,class Cont=deque<_Ty>>
	class stack
	{
	public:
		stack()//list自己会做
		{}
		~stack()//list自己会做
		{}
	public:
		bool empty()const
		{
			return _C.empty();
		}
		size_t size()const
		{
			return _C.size();
		}
		_Ty& top()
		{
			return _C.back();
		}
		void push(const _Ty &x)
		{
			_C.push_back(x);
		}
		void pop()
		{
			_C.pop_back();
		}
	private:
		Cont _C;  //容器适配器 用deque作为stack的基础  vector也可以做到
	};

*优先级队列(priority_queue)就是大小堆!向量作为适配器。
make_heap 使一个区间成为堆结构的样子排列,不是从大到小排列
make_heap;push_heap;pop_heap;sort_heap全局函数,不依赖于任何对象堆的默认排序是大堆,可以将数字从小到大排序
仿函数的使用

void main()
{
	vector<int> iv{ 5, 3, 6, 8, 1, 90, 4 };
	for (const auto & e : iv)
		cout << e << " ";
	cout << endl;

	//greater是一个仿函数
	make_heap(iv.begin(), iv.end(), greater<int>());
	//greater是一个大于的仿函数
	//成为堆结构的样子排列,不是从大到小排列,这个是小堆
	for (const auto & e : iv)
		cout << e << " ";
	cout << endl;
}

/*

//仿函数
//plus  minus  multiplies  equal_to   less  greater
void main()
{
	plus<int> pl; //pl是一个对象

	cout << pl(10, 30) << endl;//重载了小括号,如下
	
	pl.operator()(10, 30);
	
}

对于以上操作的OJ题做法

//在未排序的数组中找到第 k 个最大的元素。
//请注意,你需要找的是数组排序后的第 k 个最大的元素,而不是第 k 个不同的元素。
class Solution 
{
public:
	int findKthLargest(vector<int>& nums, int k)
	{
		priority_queue<int> dq;  //默认大堆结构
		for (int i = 0; i<nums.size(); ++i)
			dq.push(nums[i]);//这里可以将数组从小到大入堆

		for (int i = 0; i<k - 1; ++i)
			dq.pop(); //出堆 k-1次 就可以找到

		return dq.top();
	}
};
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值