In this article, we will discuss the forwardlist::emplacefront function in C++ with its syntax and examples.
In C++ STL, a forward list implements a single linked list. The forward list , introduced in C++11, is more helpful than other types of containers for insertion, deletion, and moving operations (such as sort ) since it allows for time-constant insertion and removal of entries. The forward list varies from the list in that it simply maintains track of the location of the next element, whereas the list keeps track of both the next and previous entries.
Syntax:
It has the following syntax:
forward_list::emplace_front()
The function above is used for putting a new element into the forward-looking list container, adding the new element to the starting point of the forward list.
forwardlistname.emplace_front(value)
Parameters: The value of a parameter is the object to be added to the forward list.
Result: The argument is appended at the start of the forward list.
Examples:
Input: myList{1, 2, 3, 4, 5};
myList.emplace_front(0);
Output: myList = 0, 1, 2, 3, 4, 5
Input: myList{};
myList.emplace_front(9);
Output : myList = 9
Exceptions and Errors
- It provides a strong exception assurance, so if an exception is thrown, no modifications are made.
- The argument must be of the same type as the container, and an error will be generated.
Example 1:
Filename: forward.cpp
//Example an integer forward list
// A C++ program to demonstrate the emplace() method implementation.
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<int> mylist;
mylist.emplace_front(8);
mylist.emplace_front(9);
mylist.emplace_front(10);
mylist.emplace_front(11);
mylist.emplace_front(12);
mylist.emplace_front(13);
//The final mylist becomes 13,12,11,10,9,8
// display of the list
for (auto ite = mylist.begin(); ite != mylist.end(); ++ite)
cout << ' ' << *ite;
return 0;
}
Output:
13 12 11 10 9 8
Example 2:
Filename: ForwardInt.cpp
//Example of an String Forward list
//A C++ program to demonstrate the emplace() method implementation.
#include <forward_list>
#include <iostream>
#include <string>
using namespace std;
int main() {
forward_list<string> mylist;
mylist.emplace_front("Standards");
mylist.emplace_front("Languages");
mylist.emplace_front("Programming");
//The mylist becomes Programming Languages Standards
// display of the result
for (auto it = mylist.begin(); it != mylist.end(); ++it)
cout << ' ' << *it;
return 0;
}
Output:
Programming Languages Standards
Example 3:
Filename: ForwardList.cpp
// Example of char Forward list
// A C++ program to demonstrate the emplace() method implementation.
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<char> mylist;
mylist.emplace_front('n');
mylist.emplace_front('m');
mylist.emplace_front('l');
mylist.emplace_front('k');
mylist.emplace_front('j');
//The forwarded mylist becomes j k l m n
// display of the result
for (auto iterator = mylist.begin(); iterator != mylist.end(); ++iterator)
cout << ' ' << *iterator;
return 0;
}
Output:
j k l m n
Example 4:
Filename: forwardApplication.cpp
// A C++ program to demonstrate the emplace() method implementation.
#include <forward_list>
#include <iostream>
using namespace std;
int main() {
forward_list<int> mylist{};
mylist.emplace_front(43);
mylist.emplace_front(27);
mylist.emplace_front(93);
mylist.emplace_front(45);
mylist.emplace_front(21);
mylist.emplace_front(32);
//the mylist becomes
//32 21 45 93 27 43
// sorting function
mylist.sort();
for (auto ite = mylist.begin(); ite != mylist.end(); ++ite)
cout << ' ' << *ite;
}
Output:
21 27 32 43 45 93