list::back 和 list::front

說明如何使用 list::backlist::front Visual C++ 標準樣板程式庫 (STL) 函式。

reference back( ); 
const_reference back( ) const; 
reference front( ); 
const_reference front( ) const; 
void pop_back( ); 
void pop_front( ); 
void push_back(
   const T& x
);
void push_front(
   const T& x
);

備註

注意事項注意事項

在原型中的類別/參數名稱不相符的標頭檔中的版本。某些已修改以提高可讀性。

上一步成員函式會傳回受控制序列的最後一個元素的參考。front成員函式會傳回受控制序列的第一個元素的參考。pop_back成員函式會移除受控制序列的最後一個元素。pop_front成員函式會移除受控制序列的第一個項目。所有這些函式需要受控制的序列不能為空白。Push_back 成員函式插入具有值的項目 x 在受控制序列結尾處。Push_front 成員函式插入具有值的項目 x 受控制序列的開頭。

範例

// liststck.cpp
// compile with: /EHsc
//  This example shows how to use the various stack
//                 like functions of list.
//
// Functions:
//    list::back
//    list::front
//    list::pop_back
//    list::pop_front
//    list::push_back
//    list::push_front

#pragma warning (disable:4786)
#include <list>
#include <string>
#include <iostream>

using namespace std ;

typedef list<string> LISTSTR;

int main()
{
    LISTSTR test;

    test.push_back("back");
    test.push_front("middle");
    test.push_front("front");

    // front
    cout << test.front() << endl;

    // back
    cout << test.back() << endl;

    test.pop_front();
    test.pop_back();

    // middle
    cout << test.front() << endl;
}

Output

front
back
middle

需求

標頭: <list>

請參閱

概念

標準樣板程式庫範例