📜  C++ 中的列表——一些有用的函数

📅  最后修改于: 2022-05-13 01:55:34.763000             🧑  作者: Mango

C++ 中的列表——一些有用的函数

列表是允许非连续内存分配的序列容器。与vector相比, List的遍历速度较慢,但是一旦找到位置,插入和删除都很快。

列出有用的功能:

1. emplace(position, value) :该函数用于在指定位置插入元素。

2. emplace_back(value) :- 该函数在列表末尾添加值。它与 push_back() 的不同之处在于它直接在位置创建元素,而 push_back() 首先制作一个临时副本并从那里复制。在大多数情况下,emplace_back() 的实现速度比 push_back() 快。

3. emplace_front:该函数在列表的开头添加值。它与 push_front() 的不同之处在于它直接在位置创建元素,而 push_front() 首先制作一个临时副本并从那里复制。在大多数情况下,emplace_front() 的实现速度比 push_front() 快。

CPP
// C++ code to demonstrate the working of
// emplace(), emplace_front() and emplace_back()
#include 
#include  // for list functions
using namespace std;
  
// Driver Code
int main()
{
    // Declaring a list
    list gqlist;
  
    // Initialising list iterator
    list::iterator it = gqlist.begin();
  
    // Entering list element using emplace_back()
    for (int i = 1; i <= 5; i++)
        gqlist.emplace_back(i);
  
    // Displaying list elements
    cout << "List after emplace_back operation is : ";
    for (int& x : gqlist)
        cout << x << " ";
    cout << endl;
  
    // Entering list element using emplace_front()
    for (int i = 10; i <= 50; i += 10)
        gqlist.emplace_front(i);
  
    // Displaying list elements
    cout << "List after emplace_front operation is : ";
    for (int& x : gqlist)
        cout << x << " ";
    cout << endl;
  
    // using advance() to advance iterator position
    advance(it, 2);
  
    // inserting element at 2nd position using emplace()
    gqlist.emplace(it, 100);
  
    // Displaying list elements
    cout << "List after emplace operation is : ";
    for (int& x : gqlist)
        cout << x << " ";
    cout << endl;
  
    return 0;
}


CPP
// C++ code to demonstrate the working of
// merge() and remove_if()
#include 
#include  // for list functions
using namespace std;
  
// Driver Code
int main()
{
    // Initializing list1
    list gqlist1 = { 1, 2, 3 };
  
    // Initializing list2
    list gqlist2 = { 2, 4, 6 };
  
    // using merge() to merge list1 with list2
    gqlist1.merge(gqlist2);
  
    // Displaying list elements
    cout << "list1 after merge operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    // using remove_if() to remove odd elements
    // removes 1 and 3
    gqlist1.remove_if([](int x) { return x % 2 != 0; });
  
    // Displaying list elements
    cout << "list1 after remove_if operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    return 0;
}


CPP
// C++ code to demonstrate the working of
// unique() and splice()
#include 
#include  // for list functions
using namespace std;
  
// Driver Code
int main()
{
    // Initializing list1
    list gqlist1 = { 1, 1, 1, 2, 2, 3, 3, 4 };
  
    // Initializing list2
    list gqlist2 = { 2, 4, 6 };
  
    // Initializing list1 iterator
    list::iterator it = gqlist1.begin();
  
    // using advance() to increment iterator position
    advance(it, 3);
  
    // Displaying list elements
    cout << "list1 before unique operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    // using unique() to remove repeating elements
    gqlist1.unique();
  
    // Displaying list elements
    cout << "list1 after unique operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl << endl;
  
    // using splice() to splice list2 in list1 at position
    // it inserts list2 after 2nd position
    gqlist1.splice(it, gqlist2);
  
    // Displaying list elements
    cout << "list1 after splice operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    return 0;
}


CPP
// C++ code to demonstrate the working of
// swap()
#include 
#include  // for list functions
using namespace std;
  
// Driver Code
int main()
{
    // Initializing list1
    list gqlist1 = { 1, 2, 3, 4 };
  
    // Initializing list1
    list gqlist2 = { 2, 4, 6 };
  
    // Displaying list before swapping
    cout << "The contents of 1st list "
            "before swapping are : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
    cout << "The contents of 2nd list "
            "before swapping are : ";
    for (int& x : gqlist2)
        cout << x << " ";
    cout << endl;
  
    // Use of swap() to swap the list
    gqlist1.swap(gqlist2);
  
    // Displaying list after swapping
    cout << "The contents of 1st list "
            "after swapping are : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    cout << "The contents of 2nd list "
            "after swapping are : ";
    for (int& x : gqlist2)
        cout << x << " ";
    cout << endl;
  
    return 0;
}


输出
List after emplace_back operation is : 1 2 3 4 5 
List after emplace_front operation is : 50 40 30 20 10 1 2 3 4 5 
List after emplace operation is : 50 100 40 30 20 10 1 2 3 4 5 

4. merge(list2) :此函数用于将 list2 与 list1 合并。如果两个列表都已排序,则结果列表也已排序。

5. remove_if(condition) :此函数根据其参数中给出的条件从列表中删除元素

CPP

// C++ code to demonstrate the working of
// merge() and remove_if()
#include 
#include  // for list functions
using namespace std;
  
// Driver Code
int main()
{
    // Initializing list1
    list gqlist1 = { 1, 2, 3 };
  
    // Initializing list2
    list gqlist2 = { 2, 4, 6 };
  
    // using merge() to merge list1 with list2
    gqlist1.merge(gqlist2);
  
    // Displaying list elements
    cout << "list1 after merge operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    // using remove_if() to remove odd elements
    // removes 1 and 3
    gqlist1.remove_if([](int x) { return x % 2 != 0; });
  
    // Displaying list elements
    cout << "list1 after remove_if operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    return 0;
}
输出
list1 after merge operation is : 1 2 2 3 4 6 
list1 after remove_if operation is : 2 2 4 6 

6. unique() :该函数用于删除重复出现的数字。必须对列表进行排序才能执行此函数。

7. splice(position, list2):该函数用于将元素从一个列表转移到另一个列表。

CPP

// C++ code to demonstrate the working of
// unique() and splice()
#include 
#include  // for list functions
using namespace std;
  
// Driver Code
int main()
{
    // Initializing list1
    list gqlist1 = { 1, 1, 1, 2, 2, 3, 3, 4 };
  
    // Initializing list2
    list gqlist2 = { 2, 4, 6 };
  
    // Initializing list1 iterator
    list::iterator it = gqlist1.begin();
  
    // using advance() to increment iterator position
    advance(it, 3);
  
    // Displaying list elements
    cout << "list1 before unique operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    // using unique() to remove repeating elements
    gqlist1.unique();
  
    // Displaying list elements
    cout << "list1 after unique operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl << endl;
  
    // using splice() to splice list2 in list1 at position
    // it inserts list2 after 2nd position
    gqlist1.splice(it, gqlist2);
  
    // Displaying list elements
    cout << "list1 after splice operation is : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    return 0;
}
输出
list1 before unique operation is : 1 1 1 2 2 3 3 4 
list1 after unique operation is : 1 2 3 4 

list1 after splice operation is : 1 2 4 6 2 3 4 

8. swap(list2):此函数用于将一个列表元素与另一个交换

CPP

// C++ code to demonstrate the working of
// swap()
#include 
#include  // for list functions
using namespace std;
  
// Driver Code
int main()
{
    // Initializing list1
    list gqlist1 = { 1, 2, 3, 4 };
  
    // Initializing list1
    list gqlist2 = { 2, 4, 6 };
  
    // Displaying list before swapping
    cout << "The contents of 1st list "
            "before swapping are : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
    cout << "The contents of 2nd list "
            "before swapping are : ";
    for (int& x : gqlist2)
        cout << x << " ";
    cout << endl;
  
    // Use of swap() to swap the list
    gqlist1.swap(gqlist2);
  
    // Displaying list after swapping
    cout << "The contents of 1st list "
            "after swapping are : ";
    for (int& x : gqlist1)
        cout << x << " ";
    cout << endl;
  
    cout << "The contents of 2nd list "
            "after swapping are : ";
    for (int& x : gqlist2)
        cout << x << " ";
    cout << endl;
  
    return 0;
}
输出
The contents of 1st list before swapping are : 1 2 3 4 
The contents of 2nd list before swapping are : 2 4 6 
The contents of 1st list after swapping are : 2 4 6 
The contents of 2nd list after swapping are : 1 2 3 4