deque::front 和 deque::back

在 Visual C++ 演示如何使用 、向量、双端队列:: 前面、向量、双端队列:: 返回 标准 (STL)模板库函数。

reference front( );
   const_reference front( ) const;
reference back( );
   const_reference back( ) const;

备注

备注

类/参数名在原型不匹配版本在头文件。修改某些提高可读性。

front 成员函数返回对控件序列的第一个元素,必须为非 null。 返回 成员函数返回对控件序列的最后一个元素,必须为非 null。

示例

// front.cpp
// compile with: /EHsc
#include <iostream>
#include <deque>
using namespace std;
typedef deque<char >  CHARDEQUE;
void print_contents (CHARDEQUE  deque, char*);

int main()
{
   //create a  with  A, B, C and D
   CHARDEQUE  a;
   a.push_back('A');
   a.push_back('B');
   a.push_back('C');
   a.push_back('D');

   //print out the contents
   print_contents (a,"a");
   cout <<"The first element of a is " <<a.front() <<endl;
   cout <<"The last element of a is " <<a.back() <<endl;

   // modify first and last elements using reference, front, and back
   CHARDEQUE::reference reffront=a.front();
   CHARDEQUE::reference refback=a.back();

   reffront='X';
   refback='Y';
   print_contents (a,"a");
}

// print the contents of deque
void print_contents (CHARDEQUE  deque, char *name)
{
   CHARDEQUE::iterator pdeque;
   cout << "The contents of " << name << ":";
   for (pdeque = deque.begin(); pdeque != deque.end(); pdeque++)
      cout << " " << *pdeque;

   cout<<endl;
}
  

要求

标题: deque

请参见

概念

标准模板库示例