ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

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 years. 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

    This website uses cookies

    As a user in the EEA, your approval is needed on a few things. To provide a better website experience, hubpages.com uses cookies (and other similar technologies) and may collect, process, and share personal data. Please choose which areas of our service you consent to our doing so.

    For more information on managing or withdrawing consents and how we handle data, visit our Privacy Policy at: https://hubpages.com/privacy-policy#gdpr

    Show Details
    Necessary
    HubPages Device IDThis is used to identify particular browsers or devices when the access the service, and is used for security reasons.
    LoginThis is necessary to sign in to the HubPages Service.
    Google RecaptchaThis is used to prevent bots and spam. (Privacy Policy)
    AkismetThis is used to detect comment spam. (Privacy Policy)
    HubPages Google AnalyticsThis is used to provide data on traffic to our website, all personally identifyable data is anonymized. (Privacy Policy)
    HubPages Traffic PixelThis is used to collect data on traffic to articles and other pages on our site. Unless you are signed in to a HubPages account, all personally identifiable information is anonymized.
    Amazon Web ServicesThis is a cloud services platform that we used to host our service. (Privacy Policy)
    CloudflareThis is a cloud CDN service that we use to efficiently deliver files required for our service to operate such as javascript, cascading style sheets, images, and videos. (Privacy Policy)
    Google Hosted LibrariesJavascript software libraries such as jQuery are loaded at endpoints on the googleapis.com or gstatic.com domains, for performance and efficiency reasons. (Privacy Policy)
    Features
    Google Custom SearchThis is feature allows you to search the site. (Privacy Policy)
    Google MapsSome articles have Google Maps embedded in them. (Privacy Policy)
    Google ChartsThis is used to display charts and graphs on articles and the author center. (Privacy Policy)
    Google AdSense Host APIThis service allows you to sign up for or associate a Google AdSense account with HubPages, so that you can earn money from ads on your articles. No data is shared unless you engage with this feature. (Privacy Policy)
    Google YouTubeSome articles have YouTube videos embedded in them. (Privacy Policy)
    VimeoSome articles have Vimeo videos embedded in them. (Privacy Policy)
    PaypalThis is used for a registered author who enrolls in the HubPages Earnings program and requests to be paid via PayPal. No data is shared with Paypal unless you engage with this feature. (Privacy Policy)
    Facebook LoginYou can use this to streamline signing up for, or signing in to your Hubpages account. No data is shared with Facebook unless you engage with this feature. (Privacy Policy)
    MavenThis supports the Maven widget and search functionality. (Privacy Policy)
    Marketing
    Google AdSenseThis is an ad network. (Privacy Policy)
    Google DoubleClickGoogle provides ad serving technology and runs an ad network. (Privacy Policy)
    Index ExchangeThis is an ad network. (Privacy Policy)
    SovrnThis is an ad network. (Privacy Policy)
    Facebook AdsThis is an ad network. (Privacy Policy)
    Amazon Unified Ad MarketplaceThis is an ad network. (Privacy Policy)
    AppNexusThis is an ad network. (Privacy Policy)
    OpenxThis is an ad network. (Privacy Policy)
    Rubicon ProjectThis is an ad network. (Privacy Policy)
    TripleLiftThis is an ad network. (Privacy Policy)
    Say MediaWe partner with Say Media to deliver ad campaigns on our sites. (Privacy Policy)
    Remarketing PixelsWe may use remarketing pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to advertise the HubPages Service to people that have visited our sites.
    Conversion Tracking PixelsWe may use conversion tracking pixels from advertising networks such as Google AdWords, Bing Ads, and Facebook in order to identify when an advertisement has successfully resulted in the desired action, such as signing up for the HubPages Service or publishing an article on the HubPages Service.
    Statistics
    Author Google AnalyticsThis is used to provide traffic data and reports to the authors of articles on the HubPages Service. (Privacy Policy)
    ComscoreComScore is a media measurement and analytics company providing marketing data and analytics to enterprises, media and advertising agencies, and publishers. Non-consent will result in ComScore only processing obfuscated personal data. (Privacy Policy)
    Amazon Tracking PixelSome articles display amazon products as part of the Amazon Affiliate program, this pixel provides traffic statistics for those products (Privacy Policy)