[C++基础]队列<queue>中的常用函数
2013-09-14 20:26
513 查看
在C++中只要#include<queue>即可使用队列类,其中在面试或笔试中常用的成员函数如下(按照最常用到不常用的顺序)
1. push
2. pop
3. size
4. empty
5. front
6. back
接下来逐一举例说明:
1. push
队列中由于是先进先出,push即在队尾插入一个元素,如:
可以输出:Hello World!
2. pop
将队列中最靠前位置的元素拿掉,是没有返回值的void函数。如:
可以输出:China
原因是Hello World!已经被除掉了。
3. size
返回队列中元素的个数,返回值类型为unsigned int。如:
输出两行,分别为0和2,即队列中元素的个数。
4. empty
判断队列是否为空的,如果为空则返回true。如:
输出为两行,分别是1和0。因为一开始队列是空的,后来插入了两个元素。
5. front
返回值为队列中的第一个元素,也就是最早、最先进入队列的元素。注意这里只是返回最早进入的元素,并没有把它剔除出队列。如:
输出值为两行,分别是Hello World!和China。只有在使用了pop以后,队列中的最早进入元素才会被剔除。
6. back
返回队列中最后一个元素,也就是最晚进去的元素。如:
输出值为China,因为它是最后进去的。这里back仅仅是返回最后一个元素,也并没有将该元素从队列剔除掉。
其他的方法不是很常用,就不再研究了。
接下来我们使用链表,自己将queue类写出来,将其所有方法都实现。代码都是自己写的,最后随便写了点main函数小测一下,没有发现问题,如果有不足还望能指正。如下:
队列实现
1. push
2. pop
3. size
4. empty
5. front
6. back
接下来逐一举例说明:
1. push
队列中由于是先进先出,push即在队尾插入一个元素,如:
queue<string> q; q.push("Hello World!"); q.push("China"); cout<<q.front()<<endl;
可以输出:Hello World!
2. pop
将队列中最靠前位置的元素拿掉,是没有返回值的void函数。如:
queue<string> q; q.push("Hello World!"); q.push("China"); q.pop(); 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。如:
queue<string> q; cout<<q.empty()<<endl; q.push("Hello World!"); q.push("China"); cout<<q.empty()<<endl;
输出为两行,分别是1和0。因为一开始队列是空的,后来插入了两个元素。
5. front
返回值为队列中的第一个元素,也就是最早、最先进入队列的元素。注意这里只是返回最早进入的元素,并没有把它剔除出队列。如:
queue<string> q; q.push("Hello World!"); q.push("China"); cout<<q.front()<<endl;
q.pop();
cout<<q.front()<<endl;
输出值为两行,分别是Hello World!和China。只有在使用了pop以后,队列中的最早进入元素才会被剔除。
6. back
返回队列中最后一个元素,也就是最晚进去的元素。如:
queue<string> q; q.push("Hello World!"); q.push("China"); cout<<q.back()<<endl;
输出值为China,因为它是最后进去的。这里back仅仅是返回最后一个元素,也并没有将该元素从队列剔除掉。
其他的方法不是很常用,就不再研究了。
接下来我们使用链表,自己将queue类写出来,将其所有方法都实现。代码都是自己写的,最后随便写了点main函数小测一下,没有发现问题,如果有不足还望能指正。如下:
#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++基础]队列<queue>中的常用函数
- 队列<queue>中的常用函数
- [C++基础]队列queue中的常用函数
- C++基础-string截取、替换、查找子串函数<转>
- c++中string常用函数整理<引用>
- 【C++】队列容器 头文件 #include<queue> 解读
- 【队列】C++队列头文件<queue>的应用
- C++库常用函数一览(<string><cctype><algorithm><cmath><cstdlib><iomanip><numeric>)
- C++库常用函数一览(<string><cctype><algorithm><cmath><cstdlib><iomanip><numeric>)
- C++关于<queue>的一些函数
- c++ <string.h>中包括哪些常用函数
- C++库常用函数一览(<string><cctype><algorithm><cmath><cstdlib><iomanip><numeric>)
- C++库常用函数一览 :<string> <cctype> <algorithm> <cmath> <cstdlib> <iomanip> <numeric>
- [C++STDlib基础]关于数学函数的介绍——C++标准库头文件<cmath>
- C++基础:C++标准库之栈(stack)和队列(queue)
- #include <algorithm> 常用函数
- C <string.h>常用函数介绍
- C/C++中常用库函数-<math.h>
- c\c++ 时间相关函数 include<time.h>
- priority_queue<int,vector<int>,greater<int>>优先队列 按照由小到大顺序