常用STL类的使用
2016-07-14 00:16
323 查看
栈 satck
队列 queue
优先队列 priority_queue
1.栈(stack)
(1)定义:只允许在一端插入和删除的线性表;
(2)栈顶(top):允许插入和删除的一端;
(3)特点:后进先出(LIFO)。
(4)引用头文件:#include<stack>
(5)成员函数:
empty 测试stack是否为空;
pop 从stack的顶部移除元素;
push 将元素添加到stack顶部;
size 返回stack中的元素个数;
top 返回stack顶部元素。
(6)使用例子:
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int i;
int num;
stack<int> st; //声明一个存储int型的栈,stack是标准库中的一个容器适配器,
//是个类模板,使用的时候需要实例化,int是模板实参。
st.push(10); //将元素添加到stack顶部;
st.push(20);
st.push(30);
i = st.top(); //返回stack顶部元素
cout << "The element at the top of the stack is:" << i << endl;
num = st.size(); //返回stack中的元素个数
cout << "The size of the stack is:" << num << endl;
st.pop(); //从stack的顶部移除元素
i = st.top();
cout << "The element at the top of the stack is:" << i << endl;
return 0;
}
2.队列(queue)
(1)定义:队列是只允许在一端删除,在另一端插入的线性表;
队头(front):允许删除的一端;
队尾(rear):允许插入的一端;
(2)特性:先进先出(FIFO,First In First Out);
(3)引用头文件:#include<queue>
(4)成员函数:
back(): Returns a reference to the last and most recently added element
at the back of the queue.(返回队列中最后和最近添加的元素)
empty(): Tests if the queue is empty.(测试队列是否为空)
front(): Returns a reference to the first element at the front of the queue.(返回队列中的第一个元素)
pop(): Removes an element from the front of the queue.(删除队列第一个元素)
push(): Adds an element to the back of the queue.(将元素插入到队尾)
size(): Returns the number of the element in the queue.(返回队列的元素个数)
(5)使用例子:
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int i;
int num;
queue<int> st;
st.push(10); //将元素插入到队尾
st.push(20);
st.push(30);
i = st.front(); //返回队列中的第一个元素
cout << "the element at the front of the queue is:" << i << endl;
i = st.back(); //返回队列中最后和最近添加的元素
cout << "the element at the back of the queue is:" << i << endl;
num = st.size(); //返回队列的元素个数
cout << "the number of the element in the queue is:" << i << endl;
st.pop(); //删除队列第一个元素
i = st.front();
cout << "the element at the front of the queue is:" << i << endl;
if (!st.empty()) //测试队列是否为空
cout << "The queue is not empty!" << endl;
else
cout << "The queue is empty!" << endl;
return 0;
}
3.优先队列(priority_queue)
(1)定义:是一种最高级先出 (largest-in,first-out)的数据结构;
(2)特点:最高级先出 (largest-in,first-out);
(3)引用头文件:#include<queue>;
(4)成员函数:
empty 测试priority_queue是否为空;
pop 从priority_queue顶部位置移除最大的元素。
push 将元素添加到基于元素优先级的优先队列中;
size 返回priority_queue的元素个数;
top 返回优先队列中的最大元素。
(5)元素优先级的确定:默认的元素比较器是less<T>,即元素值大则优先;
(6)使用例子:
#include<iostream>
#include<queue>
using namespace std;
int main()
{
priority_queue<int> st; //声明一个存储int型的优先队列
st.push(30); //将元素添加到基于元素优先级的优先队列中;
st.push(10);
st.push(50);
st.push(20);
st.push(40);
while (!st.empty()) //当优先队列st不为空时
{
cout << st.top() << " "; //返回优先队列中的最大元素;
st.pop(); //从priority_queue顶部位置移除最大的元素。
}
cout << endl;
return 0;
}
(7)元素优先级的改变:改写元素比较器
最小优先队列的构建:
#include<iostream>
#include<queue>
using namespace std;
struct Node //结点的结构体定义
{
int x;
Node(int i) //构造函数
{
x = i;
}
};
bool operator <(const Node &a, const Node &b) //改写元素比较器,即元素值小优先
{
if (a.x > b.x)
return true;
else
return false;
}
int main()
{
Node n(0); //声明结点对象,并初始化
priority_queue<Node> p; //声明优先队列对象
p.push(Node(4));
p.push(Node(9));
p.push(Node(3));
while (!p.empty())
{
n = p.top();
cout << n.x << " ";
p.pop();
}
cout << endl;
return 0;
}
结果显示:
队列 queue
优先队列 priority_queue
1.栈(stack)
(1)定义:只允许在一端插入和删除的线性表;
(2)栈顶(top):允许插入和删除的一端;
(3)特点:后进先出(LIFO)。
(4)引用头文件:#include<stack>
(5)成员函数:
empty 测试stack是否为空;
pop 从stack的顶部移除元素;
push 将元素添加到stack顶部;
size 返回stack中的元素个数;
top 返回stack顶部元素。
(6)使用例子:
#include<iostream>
#include<stack>
using namespace std;
int main()
{
int i;
int num;
stack<int> st; //声明一个存储int型的栈,stack是标准库中的一个容器适配器,
//是个类模板,使用的时候需要实例化,int是模板实参。
st.push(10); //将元素添加到stack顶部;
st.push(20);
st.push(30);
i = st.top(); //返回stack顶部元素
cout << "The element at the top of the stack is:" << i << endl;
num = st.size(); //返回stack中的元素个数
cout << "The size of the stack is:" << num << endl;
st.pop(); //从stack的顶部移除元素
i = st.top();
cout << "The element at the top of the stack is:" << i << endl;
return 0;
}
2.队列(queue)
(1)定义:队列是只允许在一端删除,在另一端插入的线性表;
队头(front):允许删除的一端;
队尾(rear):允许插入的一端;
(2)特性:先进先出(FIFO,First In First Out);
(3)引用头文件:#include<queue>
(4)成员函数:
back(): Returns a reference to the last and most recently added element
at the back of the queue.(返回队列中最后和最近添加的元素)
empty(): Tests if the queue is empty.(测试队列是否为空)
front(): Returns a reference to the first element at the front of the queue.(返回队列中的第一个元素)
pop(): Removes an element from the front of the queue.(删除队列第一个元素)
push(): Adds an element to the back of the queue.(将元素插入到队尾)
size(): Returns the number of the element in the queue.(返回队列的元素个数)
(5)使用例子:
#include<iostream>
#include<queue>
using namespace std;
int main()
{
int i;
int num;
queue<int> st;
st.push(10); //将元素插入到队尾
st.push(20);
st.push(30);
i = st.front(); //返回队列中的第一个元素
cout << "the element at the front of the queue is:" << i << endl;
i = st.back(); //返回队列中最后和最近添加的元素
cout << "the element at the back of the queue is:" << i << endl;
num = st.size(); //返回队列的元素个数
cout << "the number of the element in the queue is:" << i << endl;
st.pop(); //删除队列第一个元素
i = st.front();
cout << "the element at the front of the queue is:" << i << endl;
if (!st.empty()) //测试队列是否为空
cout << "The queue is not empty!" << endl;
else
cout << "The queue is empty!" << endl;
return 0;
}
3.优先队列(priority_queue)
(1)定义:是一种最高级先出 (largest-in,first-out)的数据结构;
(2)特点:最高级先出 (largest-in,first-out);
(3)引用头文件:#include<queue>;
(4)成员函数:
empty 测试priority_queue是否为空;
pop 从priority_queue顶部位置移除最大的元素。
push 将元素添加到基于元素优先级的优先队列中;
size 返回priority_queue的元素个数;
top 返回优先队列中的最大元素。
(5)元素优先级的确定:默认的元素比较器是less<T>,即元素值大则优先;
(6)使用例子:
#include<iostream>
#include<queue>
using namespace std;
int main()
{
priority_queue<int> st; //声明一个存储int型的优先队列
st.push(30); //将元素添加到基于元素优先级的优先队列中;
st.push(10);
st.push(50);
st.push(20);
st.push(40);
while (!st.empty()) //当优先队列st不为空时
{
cout << st.top() << " "; //返回优先队列中的最大元素;
st.pop(); //从priority_queue顶部位置移除最大的元素。
}
cout << endl;
return 0;
}
(7)元素优先级的改变:改写元素比较器
最小优先队列的构建:
#include<iostream>
#include<queue>
using namespace std;
struct Node //结点的结构体定义
{
int x;
Node(int i) //构造函数
{
x = i;
}
};
bool operator <(const Node &a, const Node &b) //改写元素比较器,即元素值小优先
{
if (a.x > b.x)
return true;
else
return false;
}
int main()
{
Node n(0); //声明结点对象,并初始化
priority_queue<Node> p; //声明优先队列对象
p.push(Node(4));
p.push(Node(9));
p.push(Node(3));
while (!p.empty())
{
n = p.top();
cout << n.x << " ";
p.pop();
}
cout << endl;
return 0;
}
结果显示:
相关文章推荐
- 浅析STL中的常用算法
- STL区间成员函数及区间算法总结
- C#队列Queue用法实例分析
- c++ STL容器总结之:vertor与list的应用
- C++在成员函数中使用STL的find_if函数实例
- 用PHP写的基于Memcache的Queue实现代码
- 关于STL中list容器的一些总结
- C#队列Queue多线程用法实例
- linux中编写自己的并发队列类(Queue 并发阻塞队列)
- 关于STL中的map容器的一些总结
- vector,map,list,queue的区别详细解析
- 浅析stl序列容器(map和set)的仿函数排序
- STL list链表的用法详细解析
- stl容器set,map,vector之erase用法与返回值详细解析
- STl中的排序算法详细解析
- 关于STL中vector容器的一些总结
- 关于STL中set容器的一些总结
- Laravel 4.2 中队列服务(queue)使用感受
- jQuery中队列queue()函数的实例教程
- Python Queue模块详解