
- C++ Basics
- C++ Home
- C++ Overview
- C++ Environment Setup
- C++ Basic Syntax
- C++ Comments
- C++ Data Types
- C++ Variable Types
- C++ Variable Scope
- C++ Constants/Literals
- C++ Modifier Types
- C++ Storage Classes
- C++ Operators
- C++ Loop Types
- C++ Decision Making
- C++ Functions
- C++ Numbers
- C++ Arrays
- C++ Strings
- C++ Pointers
- C++ References
- C++ Date & Time
- C++ Basic Input/Output
- C++ Data Structures
- C++ Object Oriented
- C++ Classes & Objects
- C++ Inheritance
- C++ Overloading
- C++ Polymorphism
- C++ Abstraction
- C++ Encapsulation
- C++ Interfaces
list::emplace_front() and list::emplace_back() in C++ STL
In this article we will be discussing the working, syntax and examples of list::emplace_front() and list::emplace_back() function in C++ STL.
What is a List in STL?
List is a data structure that allows constant time insertion and deletion anywhere in sequence. Lists are implemented as doubly linked lists. Lists allow non-contiguous memory allocation. List performs better insertion extraction and moving of element in any position in container than array, vector and deque. In List the direct access to the element is slow and list is similar to forward_list, but forward list objects are single linked lists and they can only be iterated forwards.
What is the list::emplace_front()?
list::emplace_front() is an inbuilt function in C++ STL which is declared in <list> header file. emplace_front() is used to emplace (insert) the element at the beginning of the list container. If the container is empty, it pushes the element at the first position and the element becomes the first element and if the container is having the elements beforehand the function inserts the element passed to it to the front and the existing element which is on first position will become the second element. This function increases the size of the container by 1.
Syntax
listname.emplace_front (const value_type& element1); listname.emplace_front (value_type&& element1);
Parameter
This function accepts only 1 element which is to be emplaced/inserted.
Return Value
This function returns nothing.
Example
Input: list<int> mylist = {1, 2, 3, 4}; mylist.emplace_front(0) Output: List elements are = 0 1 2 3 4
Example
#include <iostream> #include <list> using namespace std; int main(){ list<int> List; List.emplace_front(10); List.emplace_front(20); List.emplace_front(30); List.emplace_front(40); List.emplace_front(50); List.emplace_front(60); cout<<"Elements are : "; for(auto i = List.begin(); i!= List.end(); ++i) cout << ' ' << *i; return 0; }
Output
If we run the above code it will generate the following output −
Elements are : 60 50 40 30 20 10
What is the list::emplace_back()?
list::emplace_back() is an inbuilt function in C++ STL which is declared in <list> header file. emplace_back() is used to emplace (insert) the element at the back or at the end of the list container. If the container is empty, it just simply inserts the element and the size of the container becomes 1 if the container is having the elements beforehand the function inserts the element passed to it at the end of the list container. This function increases the size of the container by 1.
Syntax
listname.emplace_back(const value_type& element1); listname.emplace_back(value_type&& element1);
Parameter
This function accepts only 1 element which is to be emplaced/inserted.
Return Value
This function returns nothing.
Example
Input: list<int> list1 = {1, 2, 3, 4}; list1.emplace_back(5); Output: List: 1 2 3 4 5
Example
#include <iostream> #include <list> using namespace std; int main(){ list<int> List; List.emplace_back(10); List.emplace_back(20); List.emplace_back(30); List.emplace_back(40); List.emplace_back(50); List.emplace_back(60); cout<<"elements are : "; for(auto i=List.begin(); i!= List.end(); ++i) cout << ' ' << *i; return 0; }
Output
If we run the above code it will generate the following output −
Elements are : 10 20 30 40 50 60
- Related Articles
- Deque emplace_front( ) and deque emplace_back( ) in C++ in STL
- forward_list emplace_after() and emplace_front() in C++ STL
- list begin( ) and list end( ) in C++ STL
- list::front() and list::back() in C++ STL
- list::pop_front() and list::pop_back() in C++ STL
- list::push_front() and list::push_back() in C++ STL
- list_remove( ) and list remove_if( )in C++ STL
- list unique( ) in C++ STL
- list insert( ) in C++ STL
- List::clear() in C++ STL
- list get_allocator in C++ STL
- list swap( ) in C++ STL
- list operator = in C++ STL
- list cbegin() and cend() functions in C++ STL
- List crbegin() and crend() function in C++ STL
