ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel
  • »
  • Technology»
  • Computers & Software»
  • Computer Science & Programming»
  • Programming Languages

Use of pop_front(), pop_back(), front() and back() functions of c++ List

Updated on November 16, 2017
sirama profile image

I am a software engineer. I have been working with C++, MFC, and .net technologies for 15 Yrs. I like playing video games & reading books.

1. Introduction

In this hub, we are going to see list operations front(), back(), pop_front(), pop_back() and size(). After this, we are going to see an example of using the "list allocator". OK, let us start.

2. The list::pop_front() and list::pop_back() functions

We know that push_back() and push_front() will add elements to the list. The pop functions are used to remove an element from the list. The pop_front() function removes the first element in the list. Whereas the pop_back() will remove the last element in the list. Now have a look at the code below:

//Sample 1.0 Declare and Add elements to list
list<int> theList;
theList.push_back(2);
theList.push_back(4);
theList.push_back(6);
theList.push_back(8);
theList.push_back(10);

//Sample 3.0 Iterate through the list
list<int>::iterator list_itr;
printf("The list elements are:\n");
list_itr = theList.begin();
for (list_itr; list_itr != theList.end(); list_itr++)
	printf("[%d] ", *list_itr);

//Sample 4.0 Pop_Front and Pop_Back
theList.pop_back();
printf("\nAfter calling pop_back() once, The list elements are:\n");
list_itr = theList.begin();
for (list_itr; list_itr != theList.end(); list_itr++)
	printf("[%d] ", *list_itr);

theList.pop_front();
printf("\nAfter calling pop_back() once, The list elements are:\n");
list_itr = theList.begin();
for (list_itr; list_itr != theList.end(); list_itr++)
	printf("[%d] ", *list_itr);	

In the above example, five elements are added to the list so that the list contains 2,4,6,8,10. When we call the pop_front() function, the first element which holds value 2 will be removed. The same way, when we call pop_back() function, the last element which holds the value 10 will be removed from the list. This is shown in the below depiction:

Pic1. pop_front() and pop_back() on List
Pic1. pop_front() and pop_back() on List | Source

And the output of the above code is shown below:

Pic 2. Output of Pop operation
Pic 2. Output of Pop operation | Source

3. The list::front() and list::back()

The front() and back() function returns the reference to the first and last element. Since it returns reference, one can make modification to the returned element and see the changes on the list itself. Have a look at the code snippet below:

//Sample 4.0 fornt and back returns reference to First and last elements respectively
int x = theList.front();
int y = theList.back();
printf("\n\nFront Element: %d, Back Element: %d\n\n", x, y);

Here, we are getting first and last elements in variables x, y by calling front() and back() functions. Then the values are printed and in the output, one can see:

nFront Element: 2, Back Element: 10

Now have a look at the below code snippet:

//Sample 5.0 Since they return reference, let us change the front and back of the list
theList.front() = 7;
theList.back() = 17;
x = theList.front();
y = theList.back();
printf("After Changing the return value\n");
printf("Front Element: %d, Back Element: %d\n\n", x, y);

Here, the function return value changed directly. For Example, Let us consider the call “theList.front() = 7”. The function front() return reference to the first element in the list which is 2 and we are changing that to 7. After the call, the list first element’s value is replaced the new value 7. Same we did for the last element.

The complete example and its output are given below:

#include <stdio.h>
#include <conio.h>
#include <list>

using namespace std;

//Example 01
void main()
{
	//Sample 1.0 Declare and Add elements to list
	list<int> theList;
	theList.push_back(2);
	theList.push_back(4);
	theList.push_back(6);
	theList.push_back(8);
	theList.push_back(10);

	//Sample 3.0 Iterate through the list
	list<int>::iterator list_itr;
	printf("The list elements are:\n");
	list_itr = theList.begin();
	for (list_itr; list_itr != theList.end(); list_itr++)
		printf("[%d] ", *list_itr);

	//Sample 4.0 fornt and back returns reference to First and last elements respectively
	int x = theList.front();
	int y = theList.back();
	printf("\n\nFront Element: %d, Back Element: %d\n\n", x, y);

	//Sample 5.0 Since they return reference, let us change the front and back of the list
	theList.front() = 7;
	theList.back() = 17;
	x = theList.front();
	y = theList.back();
	printf("After Changing the return value\n");
	printf("Front Element: %d, Back Element: %d\n\n", x, y);

	//Sample 5.0 Let us iterate again
	printf("Let us iterate the List again:\n");
	list_itr = theList.begin();
	for (list_itr; list_itr != theList.end(); list_itr++)
		printf("[%d] ", *list_itr);

	_getch();
}

Output

Pic 3. list::front() and list::back() Example - Program Output
Pic 3. list::front() and list::back() Example - Program Output | Source

4. Closure Note

So, In summary, the pop functions actually remove the elements from the C++ Std::List. Whereas, the front() and top() returns the references and it will not remove the elements from the list.

Comments

    0 of 8192 characters used
    Post Comment

    No comments yet.

    working