- 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.
推薦閱讀
- C及C++程序設(shè)計(jì)(第4版)
- Java程序設(shè)計(jì)(慕課版)
- Microsoft Exchange Server PowerShell Cookbook(Third Edition)
- Python 3.7網(wǎng)絡(luò)爬蟲快速入門
- Java異步編程實(shí)戰(zhàn)
- 精通Linux(第2版)
- C語言程序設(shè)計(jì)
- C語言程序設(shè)計(jì)
- Learning Apache Karaf
- Java SE實(shí)踐教程
- 小程序從0到1:微信全棧工程師一本通
- 微前端設(shè)計(jì)與實(shí)現(xiàn)
- Java程序設(shè)計(jì)教程
- Java EE輕量級解決方案:S2SH
- Python繪圖指南:分形與數(shù)據(jù)可視化(全彩)