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

none

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


 

原创粉丝点击