Was this page helpful?
Your feedback about this content is important. Let us know what you think.
Additional feedback?
1500 characters remaining
Export (0) Print
Expand All
abs
Expand Minimize
Important This document may not represent best practices for current development, links to downloads and other resources may no longer be valid. Current recommended version can be found here.

list::back and list::front 

Illustrates how to use the list::back and list::front Standard Template Library (STL) functions in Visual C++.


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
);

NoteNote

The class/parameter names in the prototype do not match the version in the header file. Some have been modified to improve readability.

The back member function returns a reference to the last element of the controlled sequence. The front member function returns a reference to the first element of the controlled sequence. The pop_back member function removes the last element of the controlled sequence. The pop_front member function removes the first element of the controlled sequence. All these functions require that the controlled sequence be nonempty. The push_back member function inserts an element with value x at the end of the controlled sequence. The push_front member function inserts an element with value x at the beginning of the controlled sequence.

// 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;
}

front
back
middle

Header: <list>

Community Additions

ADD
Show:
© 2015 Microsoft