STL学习笔记——1.queue

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 its underlyingcontainer, providing a specific set of member functions to access itselements. Elements are pushed 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,即First In First Out,也就是先入先出。主要形式要注意:

头文件 #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 the queue is empty: i.e.whether its size is zero.

This member function effectively calls member
empty of the underlyingcontainer object.

Parameters

None

Return Value

true if the underlyingcontainer's size is 0, false otherwise.

即判断队列是否为空。这样说比较抽象,还是直接用代码说明吧。

// queue::empty
#include <iostream>       // std::cout
#include <queue>          // std::queue

int 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 the queue , 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 the underlying 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::queue

int 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 the queue.

The next element
isthe "oldest" element in the queue and the same element that is popped out from the queue when queue::pop is called.

This member function effectively calls member
front of the underlyingcontainer object.


Parameters

none


Return value

A reference to the next element in the queue.

·        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::queue

int 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 the queue. 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 the queue.

·        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::queue

int 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 the queue.

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::queue

int 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 of x.

This member function calls the non-member function swap(unqualified) to swap the underlying containers.

The
noexcept specifier matches the swapoperation on the underlying container.


Parameters

x

Another queue container adaptor of the same type (i.e., instantiated with thesame template parameters, T and Container). Sizesmay differ.



Return value

none

swap就是交换,交换两个队列:

// queue::swap
#include <iostream>       // std::cout
#include <queue>          // std::queue

int 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


 

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值