📜  C++中的转发列表|设置2(操作功能)

📅  最后修改于: 2021-05-30 11:03:46             🧑  作者: Mango

C++中的转发列表|设置1(简介和重要功能)

本文讨论了更多功能

除了插入和删除之外,可以在转发列表中使用的一些其他操作如下:

1. merge() :-此函数用于将一个转发列表与其他列表合并。如果两个列表都已排序,则返回的结果列表也将被排序。

2.运算符“ =” :-此运算符一个转发列表复制到另一个列表中。在这种情况下制作的副本是深层副本。

// C++ code to demonstrate the working of 
// merge() and operator=
#include
#include 
using namespace std;
  
int main()
{     
     // Initializing 1st forward list
     forward_list flist1 = {1, 2, 3};
       
     // Declaring 2nd forward list
     forward_list flist2; 
       
     // Creating deep copy using "="
     flist2 = flist1;
       
     // Displaying flist2
     cout << "The contents of 2nd forward list"
             " after copy are : ";
     for (int &x : flist2) 
         cout << x << " ";
     cout << endl;
       
     // Using merge() to merge both list in 1
     flist1.merge(flist2);
       
     // Displaying merged forward list
     // Prints sorted list
     cout << "The contents of forward list "
             "after merge are : ";
     for (int &x : flist1) 
        cout << x << " ";
     cout << endl;
       
     return 0;    
}

输出:

The contents of 2nd forward list after copy are : 1 2 3 
The contents of forward list after merge are : 1 1 2 2 3 3 

3. sort() :-此函数用于对转发列表进行排序。

4. unique() :-此函数删除多次出现的数字,并返回带有唯一元素的前向列表。应该对转发列表进行排序,以使该函数成功执行。

// C++ code to demonstrate the working of
// sort() and unique()
#include
#include // for sort() and unique()
using namespace std;
  
int main()
{
     // Initializing 1st forward list
     forward_list flist1 = {1, 2, 3, 2, 3, 3, 1};
  
     // Sorting the forward list using sort()
     flist1.sort();
  
     // Displaying sorted forward list
     cout << "The contents of forward list after "
             "sorting are : ";
     for (int &x : flist1)
         cout << x << " ";
     cout << endl;
  
     // Use of unique() to remove repeated occurrences
     flist1.unique();
  
     // Displaying forward list after using unique()
     cout << "The contents of forward list after "
             "unique operation are : ";
     for (int &x : flist1)
         cout << x << " ";
     cout << endl;
  
     return 0;
}

输出:

The contents of forward list after sorting are : 1 1 2 2 3 3 3 
The contents of forward list after unique operation are : 1 2 3 

5. reverse() :-此函数用于反向转发列表。

6. swap() :-此函数将一个转发列表的内容与其他列表交换。

// C++ code to demonstrate the working of
// reverse() and swap()
#include
#include // for reverse() and swap()
using namespace std;
int main()
{
     // Initializing 1st forward list
     forward_list flist1 = {1, 2, 3,};
  
     // Initializing 2nd forward list
     forward_list flist2 = {4, 5, 6};
  
     // Using reverse() to reverse 1st forward list
     flist1.reverse();
  
     // Displaying reversed forward list
     cout << "The contents of forward list after"
             " reversing are : ";
     for (int &x : flist1)
         cout << x << " ";
     cout << endl << endl;
  
     // Displaying forward list before swapping
     cout << "The contents of 1st forward list "
             "before swapping are : ";
     for (int &x : flist1)
         cout << x << " ";
     cout << endl;
     cout << "The contents of 2nd forward list "
             "before swapping are : ";
     for (int &x : flist2)
         cout << x << " ";
     cout << endl;
  
     // Use of swap() to swap the list
     flist1.swap(flist2);
  
     // Displaying forward list after swapping
     cout << "The contents of 1st forward list "
             "after swapping are : ";
     for (int &x : flist1)
         cout << x << " ";
     cout << endl;
  
     cout << "The contents of 2nd forward list "
             "after swapping are : ";
     for (int &x : flist2)
         cout << x << " ";
     cout << endl;
  
     return 0;
}

输出:

The contents of forward list after reversing are : 3 2 1 

The contents of 1st forward list before swapping are : 3 2 1 
The contents of 2nd forward list before swapping are : 4 5 6 
The contents of 1st forward list after swapping are : 4 5 6 
The contents of 2nd forward list after swapping are : 3 2 1 

7. clear() :-此函数清除转发列表的内容。使用此函数,转发列表将变为空。

8. empty() :-如果列表为空,则此函数返回true,否则返回false。

// C++ code to demonstrate the working of 
// clear() and empty()
#include
#include // for clear() and empty()
using namespace std;
int main()
{     
     // Initializing  forward list
     forward_list flist1 = {1, 2, 3,};
       
     // Displaying forward list before clearing
     cout << "The contents of forward list  are : ";
     for (int &x : flist1) 
         cout << x << " ";
     cout << endl;
       
     // Using clear() to clear the forward list 
     flist1.clear();
       
     // Displaying list after clear() performed
     cout << "The contents of forward list after "
          << "clearing are : ";
     for (int &x : flist1) 
         cout << x << " ";
     cout << endl;
       
     // Checking if list is empty
     flist1.empty() ? cout << "Forward list is empty" : 
                      cout << "Forward list is not empty";
      
     return 0;    
}

输出:

The contents of forward list  are : 1 2 3 
The contents of forward list after clearing are : 
Forward list is empty

有关forward_list的最新文章

要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”