[C++] STL - introduction and use of list, introduction and use of list addition, deletion and modification functions, push_back, pop_back

1. The use of list

Introduction and use of list constructor

2. Add, delete, check and modify function of list

insert image description here

(1) push_front inserts an element whose value is val before the first element of the list

  The push_front() function is used to insert a new element at the beginning of the linked list. By calling the push_front() function and passing the element to be inserted as a parameter to this function, the operation of inserting a new element at the beginning of the linked list can be realized.

  Like the insertion of the linked list, the time complexity of the push_front() function is O(1), because the operation of inserting an element to the beginning position in the doubly linked list only involves relinking the pointer without moving other elements.

  The following is the definition and usage example of the push_front() function:

#include <iostream>
#include <list>

int main() {
    
    
    std::list<int> myList = {
    
    2, 3, 4};
    
    // 使用 push_front() 在链表开头插入元素
    myList.push_front(1);
    
    // 输出链表中的元素
    for (const auto& element : myList) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

//1 2 3 4

(2) pop_front deletes the first element in the list

  The pop_front() function is used to delete the first element of the linked list.

  The time complexity of the pop_front() function is O(1), because the operation of deleting the first element in the doubly linked list only involves the relinking of the pointer, and does not need to move other elements.

  The following is the definition and usage example of the pop_front() function:

#include <iostream>
#include <list>

int main() {
    
    
    std::list<int> myList = {
    
    1, 2, 3, 4};

    // 使用 pop_front() 删除链表的第一个元素
    myList.pop_front();

    // 输出链表中的元素
    for (const auto& element : myList) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

//2 3 4

(3) push_back inserts an element whose value is val at the end of the list

  The push_back() function is used to insert an element at the end of the linked list. By calling the push_back() function and passing the element to be inserted as a parameter to the function, the operation of inserting a new element at the end of the linked list can be realized.

  The following is the definition and usage example of the push_back() function:

#include <iostream>
#include <list>

int main() {
    
    
    std::list<int> myList = {
    
    1, 2, 3};

    // 使用 push_back() 在链表末尾插入元素
    myList.push_back(4);

    // 输出链表中的元素
    for (const auto& element : myList) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

//1 2 3 4

(4) pop_back deletes the last element in the list

  The pop_back() function is used to delete the last element in the std::list container.

  The following is an example of the use and definition of the pop_back() function:

#include <iostream>
#include <list>

int main() {
    
    
    std::list<int> myList = {
    
    1, 2, 3, 4};

    // 使用pop_back()删除链表的最后一个元素
    myList.pop_back();

    // 输出链表中的元素
    for (const auto& element : myList) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

//1 2 3

(5) insert inserts an element whose value is val in the list position

  The insert() function is used to insert one or more elements at the specified position. Inserting a new element at a specified position is achieved by providing an iterator to the insertion position and using a single element or an iterator range as an argument.

  The time complexity of the insert() function depends on the number of elements inserted. If only one element is inserted, the time complexity is O(1). If multiple elements are inserted, the time complexity is the number of elements after the insertion position. Linear complexity.

  The following is the definition and usage example of the insert() function:

  We use a range for loop to iterate over the elements in the linked list and output them. In the loop body, get the value of the current element through the element variable and output it.

#include <iostream>
#include <list>

int main() {
    
    
    std::list<int> myList = {
    
    1, 2, 3, 4};

    // 在第二个位置插入元素
    auto it = std::next(myList.begin()); // 获取迭代器指向第二个位置
    myList.insert(it, 5);

    // 在第三个位置插入多个元素
    std::list<int> newElements = {
    
    6, 7};
    it = std::next(myList.begin(), 2); // 获取迭代器指向第三个位置
    myList.insert(it, newElements.begin(), newElements.end());

    // 输出链表中的元素
    for (const auto& element : myList) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

//1 5 6 7 2 3 4

(6) erase deletes the element at the list position

  The erase() function is used to delete one or more elements from the linked list.

  The following is the definition and usage example of the erase() function:

#include <iostream>
#include <list>

int main() {
    
    
    std::list<int> myList = {
    
    1, 2, 3, 4};

    // 删除第三个位置上的元素
    auto it = std::next(myList.begin(), 2); // 获取迭代器指向第三个位置
    myList.erase(it);

    // 删除第二到第四个位置上的元素
    auto first = std::next(myList.begin(), 1); // 获取迭代器指向第二个位置
    auto last = std::next(myList.begin(), 4); // 获取迭代器指向第五个位置
    myList.erase(first, last);

    // 输出链表中的元素
    for (const auto& element : myList) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

//1 4

(7) swap exchanges the elements in the two lists

  The swap() function is used to exchange the values ​​of two objects.

  The following is the definition and usage example of the swap() function:

#include <iostream>
#include <vector>

int main() {
    
    
    int a = 5;
    int b = 10;

    // 使用 swap() 函数交换两个整数值
    std::swap(a, b);

    std::cout << "a: " << a << std::endl;
    std::cout << "b: " << b << std::endl;

    std::vector<int> vec1 = {
    
    1, 2, 3};
    std::vector<int> vec2 = {
    
    4, 5, 6};

    // 使用 swap() 函数交换两个向量的值
    std::swap(vec1, vec2);

    std::cout << "vec1: ";
    for (const auto& element : vec1) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    std::cout << "vec2: ";
    for (const auto& element : vec2) {
    
    
        std::cout << element << " ";
    }
    std::cout << std::endl;

    return 0;
}

//a: 10
//b: 5
//vec1: 4 5 6
//vec2: 1 2 3

(8) clear clears the valid elements in the list

  The clear() function is used to empty the linked list, that is, delete all elements in the linked list.

  The time complexity of the clear() function is O(N), where N is the number of elements in the linked list. When emptying the linked list, the clear() function calls the destructor for each element to release the memory.

  The following is the definition and usage example of the clear() function:

#include <iostream>
#include <list>

int main() {
    
    
    std::list<int> myList = {
    
    1, 2, 3, 4};

    // 使用 clear() 函数清空链表
    myList.clear();

    // 输出链表中的元素个数
    std::cout << "Size of myList after clear: " << myList.size() << std::endl;

    return 0;
}

//Size of myList after clear: 0

おすすめ

転載: blog.csdn.net/Crocodile1006/article/details/131928397