STL学习笔记——1.queue
来源:互联网 发布:mac系统官方下载 编辑:程序博客网 时间:2024/05/22 12:27
STL学习笔记——1.queue
之前被学长坑了,非要模拟队列,以至于现在完全看不懂网上用STL写的代码,只有现学了(╯‵□′)╯︵┻━┻。
以下资料主要参考网站http://www.cplusplus.com/reference/queue/
classtemplate
<queue>
std::queue
template <class T, class Container =deque<T> > class queue;
FIFOqueue
queues area type of container adaptor, specifically designed to operate in a FIFO context(first-in first-out), where elements are inserted into one end of the containerand extracted from the other.
queues are implemented as containers adaptors, which are classesthat use an encapsulated object of a specific container class as itsunderlyingcontainer, providing a specific set of member functions to access itselements. Elements arepushed into the "back" of thespecific container and popped from its"front".
The underlying container may be one of the standard container class template orsome other specifically designed container class. This underlying containershall support at least the following operations:
- empty
- size
- front
- back
- push_back
- pop_front
队列的特点如上文,简单而言就是FIFO,即FirstInFirstOut,也就是先入先出。主要形式要注意:
●头文件 #include<queue>(或者直接写#include<bits/stdc++.h>)
●声明:
❶如果之前已经声明了命名空间:using namespace std;
则直接写:queue<变量类型(如int||char)>变量名称(选一个自己喜欢的就好罒ω罒)
❷如果之前没有声明命名空间,则写:
std::queue<变量类型(如int||char)>变量名称(选一个自己喜欢的就好罒ω罒)
●常用函数:
❶empty:
std::queue::empty
bool empty() const;
Test whethercontainer is empty
Returns whether thequeue is empty: i.e.whether itssize iszero.
This member function effectively calls member empty of theunderlyingcontainer object.
Parameters
None
Return Value
true if the underlyingcontainer's size is 0, false otherwise.
即判断队列是否为空。这样说比较抽象,还是直接用代码说明吧。
// queue::empty#include <iostream> // std::cout#include <queue> // std::queueint main (){ std::queue<int> myqueue; int sum (0); for (int i=1;i<=10;i++) myqueue.push(i); while (!myqueue.empty()) { sum += myqueue.front(); myqueue.pop(); } std::cout << "total: " << sum << '\n'; return 0;}
❷pop&&push
std::queue::pop
void pop();
Remove next element
Removes the next element in the queue, effectively reducing its size by one.The element removed is the "oldest" element in the queue whose value can be retrieved by calling member queue::front.This calls the removed element's destructor.This member function effectively calls the member function pop_front of the underlying container object.
std::queue::push
· C++98
· C++11
·
void push (const value_type& val);
void push (const value_type& val);
void push (value_type&& val);
Insert element
Inserts a new element at the end of thequeue, afterits current last element. The content of this new element is initialized to val.This member function effectively calls the member function push_back of theunderlying container object.
Parameters
val
Value to which the inserted element is initialized.
Member type value_type is the type of the elements in the container (defined as an aliasof the first class template parameter,T).
Return value
push是把元素推入队列,而pop与之相反是将元素赶出队列(既然这里用了赶出,肯定是队列中不再有这个元素了)
还是直接放代码:
// queue::push/pop#include <iostream> // std::cin, std::cout#include <queue> // std::queueint main (){ std::queue<int> myqueue; int myint; std::cout << "Please enter some integers (enter 0 to end):\n"; do { std::cin >> myint; myqueue.push (myint); } while (myint); std::cout << "myqueue contains: "; while (!myqueue.empty()) { std::cout << ' ' << myqueue.front(); myqueue.pop(); } std::cout << '\n'; return 0;}
❸front&&back
std::queue::front
· C++98
· C++11
·
value_type& front();
const value_type& front() const;
reference& front();
const_reference& front() const;
Access next element
Returns a reference to the nextelement in thequeue.The next element isthe "oldest" element in the queue and the same element that is popped out from the queue whenqueue::pop is called.
This member function effectively calls member front of theunderlyingcontainer object.
Parameters
none
Return value
A reference to the next element in thequeue.
· C++98
· C++11
·
Member type value_type is the type of the elements inthe container (defined as an alias of the first class template parameter,T).
如上文:front 就是排在最前面的那个元素还是很形象的╮(╯▽╰)╭。
// queue::front#include <iostream> // std::cout#include <queue> // std::queueint main (){ std::queue<int> myqueue; myqueue.push(77); myqueue.push(16); myqueue.front() -= myqueue.back(); // 77-16=61 std::cout << "myqueue.front() is now " << myqueue.front() << '\n'; return 0;}
/*Output:myqueue.front() is now 61*/
std::queue::back
· C++98
· C++11
·
value_type& back();
const value_type& back() const;
reference& back();
const_reference& back() const;
Access last element
Returns a reference to the last elementin thequeue. Thisis the "newest" element in the queue (i.e. the last element pushedinto the queue).This member function effectively calls member back of theunderlying container object.
Parameters
none
Return value
A reference to the last element in thequeue.
· C++98
· C++11
·
Member type value_type is the type of the elements inthe container (defined as an alias of the first class template parameter,T).
如上文,back与front 相反还是直接上代码:
// queue::back#include <iostream> // std::cout#include <queue> // std::queueint main (){ std::queue<int> myqueue; myqueue.push(12); myqueue.push(75); // this is now the back myqueue.back() -= myqueue.front(); std::cout << "myqueue.back() is now " << myqueue.back() << '\n'; return 0;}
/*Output:myqueue.back() is now 63*/
❹size
std::queue::size
size_type size() const;
Return size
Returns the number of elements in thequeue.
This member function effectively calls member size of theunderlying container object.
Parameters
none
Return Value
Thenumber of elements in the underlying container.
Member type size_type is an unsigned integral type.
计算队列大小?(或者是不是该称为长度)
// queue::size#include <iostream> // std::cout#include <queue> // std::queueint main (){ std::queue<int> myints; std::cout << "0. size: " << myints.size() << '\n'; for (int i=0; i<5; i++) myints.push(i); std::cout << "1. size: " << myints.size() << '\n'; myints.pop(); std::cout << "2. size: " << myints.size() << '\n'; return 0;}
Output:
0. size: 0
1. size: 5
2. size: 4
❺swap
std::queue::swap
void swap (queue& x) noexcept(/*see below*/);
Swap contents
Exchanges the contents of the container adaptor (*this
) by those ofx.
This member function calls the non-member function swap(unqualified) to swap theunderlying containers.
The noexcept
specifier matches theswapoperation on theunderlying container.
Parameters
x
Another queue container adaptor of the same type (i.e., instantiated with thesame template parameters, T andContainer). Sizesmay differ.
Return value
none
swap就是交换,交换两个队列:
// queue::swap#include <iostream> // std::cout#include <queue> // std::queueint main (){ std::queue<int> foo,bar; foo.push (10); foo.push(20); foo.push(30); bar.push (111); bar.push(222); foo.swap(bar); std::cout << "size of foo: " << foo.size() << '\n'; std::cout << "size of bar: " << bar.size() << '\n'; return 0;}
Output:
size of foo: 2
size of bar: 3
- STL学习笔记——1.queue
- STL学习笔记— —特殊容器queue
- STL学习笔记12— —特殊容器queue
- C++学习笔记stl queue
- C++ STL 学习笔记 queue
- STL学习——Stack/Queue篇
- C++ STL学习——queue
- C++ STL学习笔记6--queue
- C++ STL--queue队列容器学习笔记
- 【STL】顺序容器 — queue
- STL源码—stack,queue
- STL源码—stack,queue
- STL学习笔记— —STL简述
- queue--初步学习STL
- STL学习 - queue了解
- STL学习--queue priority_queue
- stl源码剖析 详细学习笔记stack queue
- C++—STL学习笔记
- 安卓 开发环境搭建笔记
- SQL注入攻击防御思维导图总结
- php开发微信公众号,接收粉丝过来的小视频
- PHP 获取类名
- javaScript JSON 遍历(for in)
- STL学习笔记——1.queue
- HTML-JavaScript---网页时间显示同步练习
- 从函数参数 引出 元组
- 一次关于数组作为函数参数的较真
- Android获取最新技术资讯可关注的网站
- 唯快不破:ICMP报文剖析-自己实现ping程序
- AcDbAlignedDimension setDimLinePoint
- 让不懂编程的人爱上iPhone开发(2017iOS11+Swift4+Xcode9版)-第5篇
- MySQL数据库Innodb引擎与MyIASM引擎的选择