C++ 知识回顾总结 -- queue 队列容器
2018-11-03 12:39
771 查看
一、说明
queue 是一种队列适配器,专门设计用于FIFO中操作(先进先出),元素从一端插入容器并从另一端提取。
相关API地址为:http://www.cplusplus.com/reference/queue/queue/
二、使用方法
在C++中只要#include<queue>即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序)
push、pop、size、empty、front、back
接下来逐一举例说明:
1. push
队列中由于是先进先出,push即在队尾插入一个元素,如:
1 queue<string> q; 2 q.push("Hello World!"); 3 q.push("China"); 4 cout<<q.front()<<endl;
可以输出:Hello World!
2. pop
将队列中最靠前位置的元素拿掉,是没有返回值的void函数。如:
1 queue<string> q; 2 q.push("Hello World!"); 3 q.push("China"); 4 q.pop(); 5 cout<<q.front()<<endl;
可以输出:China
原因是Hello World!已经被除掉了。
3. size
返回队列中元素的个数,返回值类型为unsigned int。如:
queue<string> q; cout<<q.size()<<endl; q.push("Hello World!"); q.push("China"); cout<<q.size()<<endl;
输出两行,分别为0和2,即队列中元素的个数。
4. empty
判断队列是否为空的,如果为空则返回true。如:
1 queue<string> q; 2 cout<<q.empty()<<endl; 3 q.push("Hello World!"); 4 q.push("China"); 5 cout<<q.empty()<<endl;
输出为两行,分别是1和0。因为一开始队列是空的,后来插入了两个元素。
5. front
返回值为队列中的第一个元素,也就是最早、最先进入队列的元素。注意这里只是返回最早进入的元素,并没有把它剔除出队列。如:
1 queue<string> q; 2 q.push("Hello World!"); 3 q.push("China"); 4 cout<<q.front()<<endl; 5 q.pop(); 6 cout<<q.front()<<endl;
输出值为两行,分别是Hello World!和China。只有在使用了pop以后,队列中的最早进入元素才会被剔除。
6. back
返回队列中最后一个元素,也就是最晚进去的元素。如:
1 queue<string> q; 2 q.push("Hello World!"); 3 q.push("China"); 4 cout<<q.back()<<endl;
输出值为China,因为它是最后进去的。这里back仅仅是返回最后一个元素,也并没有将该元素从队列剔除掉。
三、使用链表将queue实现
#include<iostream> #include<string> using namespace std; template <typename T> struct Node{ T value; Node<T> *next; Node<T>(){next = NULL;} }; template <typename T> class MyQueue{ private: unsigned int num; Node<T> *first; Node<T> *last; public: MyQueue(); ~MyQueue(); unsigned int size(); void push(T element); void pop(); bool empty(); T back(); T front(); }; template <typename T> MyQueue<T>::MyQueue(){ num = 0; first = NULL; last = NULL; } template <typename T> MyQueue<T>::~MyQueue(){ while(!empty()){ pop(); } } template <typename T> unsigned int MyQueue<T>::size(){ return num; } template <typename T> bool MyQueue<T>::empty(){ return (0==num); } template <typename T> void MyQueue<T>::push(T element){ Node<T> *temp = new Node<T>; temp->next = NULL; temp->value = element; if (0 == this->num){ first = temp; last = temp; }else{ last->next = temp; last = temp; } (this->num)++; } template <typename T> void MyQueue<T>::pop(){ if (0==this->num){ cout<<"No elements in the queue!"<<endl; }else if(1 == this->num){ delete first; first = NULL; last = NULL; this->num = 0; }else{ Node<T> *temp = first; first = first->next; delete temp; (this->num)--; } } template <typename T> T MyQueue<T>::back(){ if (0==this->num){ cout<<"No elements in the queue!"<<endl; return NULL; } return last->value; } template <typename T> T MyQueue<T>::front(){ if(0== this->num){ cout<<"No elements in the queue!"<<endl; return NULL; } return first->value; } int main(){ MyQueue<string> q; q.push("Hello world"); q.push("China"); cout<<q.front()<<endl; cout<<q.size()<<endl; cout<<q.back()<<endl; q.pop(); cout<<q.empty()<<endl; cout<<q.back()<<endl; cout<<q.front()<<endl; q.pop(); cout<<q.size()<<endl; cout<<q.empty()<<endl; system("pause"); return 0; }
相关文章推荐
- [C++ 面试基础知识总结] 顺序容器
- 【C++】容器适配器实现队列Queue的各种功能(入队、出队、判空、大小、访问所有元素等)
- C++STL之queue队列容器
- C++ STL--queue队列容器学习笔记
- C++基础知识总结与回顾--5.5共享数据的保护
- C++STL之priority_queue优先队列容器
- C/C++知识回顾 容器
- [C++ STL] 优先队列容器 priority_queue
- C++顺序容器基础知识总结
- C++ 顺序容器基础知识总结
- 【C++】STL常用容器总结之五:双端队列deque
- [C++ 面试基础知识总结] 关联容器
- C++中的队列(queue)和优先队列(priority_queue)总结
- 总结:c++几种容器的用法及栈和队列(适合自学c++的同学)
- C++ STL 有关容器部分 priority_queue 优先级队列的基本操作以及认识案例
- c++ STL学习之queue队列总结
- STL常用容器总结——queue单向队列
- C++ STL容器部分 有关queue 队列模型的 一些基本API函数的使用
- C++ 基础知识回顾总结
- C++ 知识回顾总结 -- 指针