Help and Support
 

powered byLive Search

How to use the list::back, list::front, list::pop_back, list::pop_front, list::push_back, and list::push_front STL functions in Visual C++

Article ID:158086
Last Review:January 9, 2006
Revision:5.0
This article was previously published under Q158086
On This Page

SUMMARY

The sample code below illustrates how to use the list::back, list::front, list::pop_back, list::pop_front, list::push_back, and list::push_front STL functions in Visual C++.

Back to the top

MORE INFORMATION

Required header

   <list>
				

Back to the top

Prototype

   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);
				
Note The class/parameter names in the prototype may not match the version in the header file. Some have been modified to improve readability.

Back to the top

Description

The member function back returns a reference to the last element of the controlled sequence. The member function front returns a reference to the first element of the controlled sequence. The member function pop_back removes the last element of the controlled sequence. The member function pop_front removes the first element of the controlled sequence. All the above functions require that the controlled sequence be non-empty.

The member function push_back inserts an element with value x at the end of the controlled sequence. The member function push_front inserts an element with value x at the beginning of the controlled sequence.

Back to the top

Sample code

////////////////////////////////////////////////////////////////////// 
// 
// Compile options needed: -GX
// 
// liststck.cpp :  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
// 
// Written by Andrew Bradnan
// Copyright (c) 1996 Microsoft Corporation. All rights reserved.
////////////////////////////////////////////////////////////////////// 

#include <list>
#include <string>
#include <iostream>
using namespace std;

#if _MSC_VER > 1020   // if VC++ version is > 4.2
   using namespace std;  // std c++ libs implemented in std
   #endif

typedef list<string, allocator<string> > LISTSTR;

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

}
				
The Program Output is:
front
back
middle
				

Back to the top

REFERENCES

For more information about list::back and list::front, visit the following Microsoft Developer Network (MSDN) Web site:
http://msdn.microsoft.com/library/en-us/vclang98/html/sample_listCCback__listCCfront_(STL_Sample).asp?frame=true (http://msdn.microsoft.com/library/en-us/vclang98/html/sample_listCCback__listCCfront_(STL_Sample).asp?frame=true)

Back to the top


APPLIES TO
The Standard C++ Library, when used with:
  Microsoft Visual C++ 4.2 Enterprise Edition
  Microsoft Visual C++ 5.0 Enterprise Edition
  Microsoft Visual C++ 6.0 Enterprise Edition
  Microsoft Visual C++ 4.2 Professional Edition
  Microsoft Visual C++ 5.0 Professional Edition
  Microsoft Visual C++ 6.0 Professional Edition
  Microsoft Visual C++ 6.0 Standard Edition
  Microsoft Visual C++ 2005 Express Edition
  Microsoft Visual C++ .NET 2003 Standard Edition
  Microsoft Visual C++ .NET 2002 Standard Edition

Back to the top

Keywords: 
kbhowto kbinfo kbcode KB158086

Back to the top

Article Translations

 

Other Support Options

  • Need More Help?
    Contact a Support professional by E-mail, Online or Phone.
  • Customer Service
    For non-technical assistance with product purchases, subscriptions, online services, events, training courses, corporate sales, piracy issues, and more.
  • Newsgroups
    Pose a question to other users. Discussion groups and Forums about specific Microsoft products, technologies, and services.