C++ 前向列表操作函数
本文旨在介绍C ++编程语言的标准模板库,其中我们已经看到了操作函数的利用。
由于C ++ STL非常庞大,像大海一样,因此我们在这里讨论了一些关键函数,示例merge(),operator “=,sort(),unique(),reverse()和swap操作符以及它们的代码和输出。
重要函数1:merge()和运算符”=
“
C ++代码
//below, we are writing down the C++ programming language code to demonstrate
//the concept of forwarding List in C++ manipulating functions along with the
//working of merge() and operator=
#include
#include
using namespace std;
int main() {
//the below code snippet initialises the 1st forwarding list for us
forward_list f_list_1 = {51, 52, 53};
//the below code snippet initialises the 2nd forwarding list for us
forward_list f_list_2;
// the below code snippet creates the deep copy using the equal to
// operator "=" for us
f_list_2 = f_list_1;
//the below code snippet Displays f_list_2
cout << "displaying the content we have at 2nd forward list."
" After performing the copy operations, we have are: ";
for (int &x : f_list_2)
cout << x << " ";
cout << endl;
//the below code snippet Uses merge() function to merge both list in 1
f_list_1.merge(f_list_2);
//the below code snippet displays
// merged forward list for us
// it also prints the sorted list for us
cout << "displaying the content we have at forwarding list."
" After performing the merge operations, we have are: ";
for (int &x : f_list_1)
cout << x << " ";
cout << endl;
return 0;
}
输出:
displaying the content have at the 2nd forward list after performing the copy operations we have are: 51 52 53
showing the content have at the forwarding list after completing the merge operations we have are: 51 51 52 52 53 53
重要函数2:sort()和Unique()
C++代码
//below, we are writing down the C++ programming language code to
//demonstrate
//the concept of forwarding List in C++ manipulating functions along with
//the working of
// sort() and unique()
#include
#include
using namespace std;
int main()
{
forward_list f_list_1 = {51, 72, 93, 2, 93, 93, 51};
f_list_1.sort();
cout << "displaying our content at the forward list."
" After performing the sort operations, we have are";
for (int &x : f_list_1)
cout << x << " ";
cout << endl;
f_list_1.unique();
cout << "displaying our content at the forward list."
" After performing the unique operations, we have are";
for (int &x : f_list_1)
cout << x << " ";
cout << endl;
return 0;
}
输出:
displaying the content, we have on the forward list. After performing the sort operations, we have 51 51 72 93 93 93
displaying the content, we have on the forward list. After completing the unique functions, we have 51 72 93
重要函数3: swap()
C++代码
//below, we are writing down the C++ programming language code to
//demonstrate
//the concept of forwarding List in C++ manipulating functions along with
//the working of
// reverse() and swap()
#include
#include
using namespace std;
int main()
{
//the below code snippet initialises the 1st forwarding list for us
forward_list f_list_1 = {31, 62, 73,};
//the below code snippet initialises the 2nd forwarding list for us
forward_list f_list_2 = {54, 65, 86};
// the below code snippet uses the reverse() operator to reverse 1st forward list
//for us
f_list_1.reverse();
// the below code snippet displays the reverse forward list
cout << "The contents of the forward list after."
" reversing are: ";
for (int &x : f_list_1)
cout << x << " ";
cout << endl << endl;
// Displaying forward list before swapping
cout << "The contents of 1st forward list."
"before swapping are: ";
for (int &x : f_list_1)
cout << x << " ";
cout << endl;
cout << "The contents of 2nd forward list."
"before swapping are: ";
for (int &x : f_list_2)
cout << x << " ";
cout << endl;
//here we are using of swap() to swap the list
f_list_1.swap(f_list_2);
//here, we are displaying the forward list after swapping
cout << "content of 1st forward list "
"After swapping are: ";
for (int &x : f_list_1)
cout << x << " ";
cout << endl;
cout << "content of 2nd forward list."
"After swapping are: ";
for (int &x : f_list_2)
cout << x << " ";
cout << endl;
return 0;
}
输出:
The contents of the forwarding list after reversing are: 73 62 31
The contents of 1st forward list before swapping are: 73 62 31
The contents of the 2nd forward list before changing are: 54 65 86
range of 1st forward list after changing are: 54 65 86
range of 2nd forward list after changing is: 73 62 31