📜  删除C++ STL列表中的元素

📅  最后修改于: 2021-05-30 05:47:50             🧑  作者: Mango

如何在C++ STL列表中插入元素?

本文介绍了STL列表中的删除方面。

  1. 使用list :: erase() :此函数的目的是从列表中删除元素。使用此函数可以删除范围内的单个或多个连续元素。此函数接受2个参数,即开始迭代器和结束迭代器。
    时间复杂度: O(n)其中(n是列表的大小)。
    // C++ code to demonstrate the working of erase()
      
    #include
    #include // for list operations
    using namespace std;
      
    int main()
    {
        // initializing list of integers
        list list1={10,15,20,25,30,35};
          
        // declaring list iterators
        list::iterator it = list1.begin();
        list::iterator it1 = list1.begin();
          
        // incrementing the positions of iterators
        advance(it,2);
        advance(it1,5);
          
        // printing original list
        cout << "The original list is : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
        // using erase() to erase single element
        // erases 20
        list1.erase(it);
          
        // list after deletion 1 element
        cout << "The list after deleting 1 element using erase() : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
        it = list1.begin();
          
        // incrementing the positions of iterators
        advance(it,2);
          
        // using erase() to erase multiple elements
        // erases 25,30
        list1.erase(it,it1);
          
        // list after deletion of multiple elements
        cout << "The list after deleting multiple elements using erase() : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
      
          
    }
    

    输出:

    The original list is : 10 15 20 25 30 35 
    The list after deleting 1 element using erase() : 10 15 25 30 35 
    The list after deleting multiple elements using erase() : 10 15 35 
    
  2. 使用list :: pop_front()和list :: pop_back()
    • pop_back() :此函数从列表中删除最后一个元素。这样可以将列表的大小减少1。
      时间复杂度:O(1)
    • pop_front() :此函数从列表中删除第一个元素,并移动后续元素。这样可以将列表的大小减少1。
      时间复杂度:O(1)
    // C++ code to demonstrate the working of pop_front()
    // and pop_back()
      
    #include
    #include // for list operations
    using namespace std;
      
    int main()
    {
        // initializing list of integers
        list list1={10,15,20,25,30,35};
          
        // printing original list
        cout << "The original list is : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
        // using pop_front() to erase first element of list
        // pops 10
        list1.pop_front();
          
        // list after deleting first element
        cout << "The list after deleting first element using pop_front() : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
        // using pop_back() to erase last element of list
        // pops 35
        list1.pop_back();
          
        // list after deleting last element
        cout << "The list after deleting last element using pop_back() : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
    }
    

    输出:

    The original list is : 10 15 20 25 30 35 
    The list after deleting first element using pop_front() : 15 20 25 30 35 
    The list after deleting last element using pop_back() : 15 20 25 30 
    
  3. 使用remove()和remove_if()
    • remove() :此函数删除在其参数中传递的所有出现的值。它与“ erase()”的不同之处在于“ erase()”按位置删除值,而“ remove()”则删除传递的值。列表的大小通过删除的出现次数而减少。
      时间复杂度: O(n)
    • remove_if() :此函数删除将“ true”返回给在其参数中传递的函数的出现。
      时间复杂度: O(n)
    // C++ code to demonstrate the working of remove()
    // remove_if()
      
    #include
    #include // for list operations
    using namespace std;
      
    // function to pass in argument of "remove_if()"
    bool is_div_5(const int& num) { return num%5==0;} 
      
    int main()
    {
        // initializing list of integers
        list list1={10,14,20,22,30,33,22};
          
        // printing original list
        cout << "The original list is : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
        // using remove() to delete all occurrences of 22
        list1.remove(22);
          
        // list after deleting all 22 occurrences
        cout << "The list after deleting all 22 occurrences : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
        // using remove_if() to delete multiple of 5 
        list1.remove_if(is_div_5);
          
        // list after deleting all multiples of 5
        cout << "The list after deleting all multiples of 5 : ";
        for (list::iterator i=list1.begin(); i!=list1.end(); i++)
           cout << *i << " ";
           
        cout << endl;
          
    }
    

    输出:

    The original list is : 10 14 20 22 30 33 22 
    The list after deleting all 22 occurrences : 10 14 20 30 33 
    The list after deleting all multiples of 5 : 14 33 
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”