- Mastering C++ Programming
- Jeganathan Swaminathan
- 228字
- 2021-07-02 18:28:51
Commonly used APIs in a forward_list container
The following table shows the commonly used forward_list APIs:
Let's explore one more example to get a firm understanding of the forward_list container:
#include <iostream>
#include <forward_list>
#include <iterator>
#include <algorithm>
using namespace std;
int main () {
forward_list<int> list1 = { 10, 20, 10, 45, 45, 50, 25 };
forward_list<int> list2 = { 20, 35, 27, 15, 100, 85, 12, 15 };
cout << "\nFirst list before sorting ..." << endl;
copy ( list1.begin(), list1.end(), ostream_iterator<int>(cout, "\t") );
cout << endl;
cout << "\nSecond list before sorting ..." << endl;
copy ( list2.begin(), list2.end(), ostream_iterator<int>(cout, "\t") );
cout << endl;
list1.sort();
list2.sort();
cout << "\nFirst list after sorting ..." << endl;
copy ( list1.begin(), list1.end(), ostream_iterator<int>(cout, "\t") );
cout << endl;
cout << "\nSecond list after sorting ..." << endl;
copy ( list2.begin(), list2.end(), ostream_iterator<int>(cout, "\t") );
cout << endl;
list1.merge ( list2 );
cout << "\nMerged list ..." << endl;
copy ( list1.begin(), list1.end(), ostream_iterator<int>(cout, "\t") );
cout << "\nMerged list after removing duplicates ..." << endl;
list1.unique();
copy ( list1.begin(), list1.end(), ostream_iterator<int>(cout, "\t") );
return 0;
}
The preceding code snippet is an interesting example that demonstrates the practical use of the sort(), merge(), and unique() STL algorithms.
The output can be viewed with the following command:
./a.out
The output of the program is as follows:
First list before sorting ...
10 20 10 45 45 50 25
Second list before sorting ...
20 35 27 15 100 85 12 15
First list after sorting ...
10 10 20 25 45 45 50
Second list after sorting ...
12 15 15 20 27 35 85 100
Merged list ...
10 10 12 15 15 20 20 25 27 35 45 45 50 85 100
Merged list after removing duplicates ...
10 12 15 20 25 27 35 45 50 85 100
The output and the program are pretty self-explanatory.
推薦閱讀
- 新一代通用視頻編碼H.266/VVC:原理、標準與實現
- 技術領導力:程序員如何才能帶團隊
- Oracle數據庫從入門到運維實戰
- Mastering ServiceNow(Second Edition)
- ASP.NET程序開發范例寶典
- Visual Basic 6.0程序設計實驗教程
- 硬件產品設計與開發:從原型到交付
- 實戰Python網絡爬蟲
- 虛擬現實建模與編程(SketchUp+OSG開發技術)
- PostgreSQL 12 High Availability Cookbook
- 讓Python遇上Office:從編程入門到自動化辦公實踐
- HikariCP數據庫連接池實戰
- H5頁面設計與制作(全彩慕課版·第2版)
- 基于Docker的Redis入門與實戰
- HTML 5與CSS 3權威指南(第4版·上冊)