- Mastering C++ Programming
- Jeganathan Swaminathan
- 176字
- 2021-07-02 18:28:51
Forward list
The STL's forward_list container is built on top of a singly linked list data structure; hence, it only supports navigation in the forward direction. As forward_list consumes one less pointer for every node in terms of memory and runtime, it is considered more efficient compared with the list container. However, as price for the extra edge of performance advantage, forward_list had to give up some functionalities.
The following diagram shows the internal data-structure used in forward_list:

Let's explore the following sample code:
#include <iostream>
#include <forward_list>
#include <iterator>
#include <algorithm>
using namespace std;
int main ( ) {
forward_list<int> l = { 10, 10, 20, 30, 45, 45, 50 };
cout << "\nlist with all values ..." << endl;
copy ( l.begin(), l.end(), ostream_iterator<int>(cout, "\t") );
cout << "\nSize of list with duplicates is " << distance( l.begin(), l.end() ) << endl;
l.unique();
cout << "\nSize of list without duplicates is " << distance( l.begin(), l.end() ) << endl;
l.resize( distance( l.begin(), l.end() ) );
cout << "\nlist after removing duplicates ..." << endl;
copy ( l.begin(), l.end(), ostream_iterator<int>(cout, "\t") );
cout << endl;
return 0;
}
The output can be viewed with the following command:
./a.out
The output will be as follows:
list with all values ...
10 10 20 30 45 45 50
Size of list with duplicates is 7
Size of list without duplicates is 5
list after removing duplicates ...
10 20 30 45 50
推薦閱讀
- Deploying Node.js
- Python for Secret Agents:Volume II
- Vue.js 2 and Bootstrap 4 Web Development
- oreilly精品圖書:軟件開發者路線圖叢書(共8冊)
- Django:Web Development with Python
- C語言程序設計案例式教程
- C程序設計案例教程
- Learning ELK Stack
- Python編程實戰
- Learning Raspbian
- Scala程序員面試算法寶典
- Getting Started with Hazelcast(Second Edition)
- 區塊鏈底層設計Java實戰
- Java Fundamentals
- 平面設計經典案例教程:CorelDRAW X6