📜  删除std :: map中的元素的不同方法(erase()和clear())

📅  最后修改于: 2021-05-30 08:27:43             🧑  作者: Mango

本文介绍了Maps的删除部分。

  1. 使用擦除() :擦除()用于擦除参数中提到的映射中的对,无论是其位置,其值还是数字范围。
    • delete(key) :使用其参数中提到的key擦除键值对。删除后重新排序地图。它返回删除的条目数。如果删除了不存在的密钥,则返回0。
      时间复杂度: log(n) (n是地图的大小)
    • delete(iter) :在参数中提到的迭代器指向的位置擦除该对。
      时间复杂度: log(n) (n是地图的大小)
    • delete(strt_iter,end_iter) :擦除从“ strt_iter”到“ end_iter”的成对范围
      时间复杂度: O(k) (k是地图的大小)
    // C++ code to demonstrate the working of erase()
      
    #include
    #include // for map operations
    using namespace std;
      
    int main()
    {
        // declaring map
        // of char and int
        map< char, int > mp;
          
        // declaring iterators
        map::iterator it ;
        map::iterator it1;
        map::iterator it2;
          
        // inserting values 
        mp['a']=5;
        mp['b']=10;
        mp['c']=15;
        mp['d']=20;
        mp['e']=30;
      
        // printing initial map elements
        cout << "The initial map elements are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        it = mp.begin();
          
        cout << endl;
          
        // erasing element using iterator
        // erases 2nd element
        // 'b'
        ++it;
        mp.erase(it);
          
        // printing map elements after deletion
        cout << "The map elements after 1st deletion are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << endl;
          
        // erasing element using value 
        int c = mp.erase('c');
          
        // printing map elements after deletion
        cout << "The map elements after 2nd deletion are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << "The number of elements deleted in 2nd deletion are : ";
        cout << c << endl;
          
        cout << endl;
          
        // erasing element using value 
        // key not present
        int d = mp.erase('w');
          
        // printing map elements after deletion
        cout << "The map elements after 3rd deletion are : \n";
          
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << "The number of elements deleted in 3rd deletion are : ";
        cout << d << endl;
          
        cout << endl;
          
          
        ++it;
        ++it;
          
        // erasing element using range iterator
        // deletes "d" and "e" keys
        mp.erase(it, mp.end());
          
        // printing map elements 4th deletion
        cout << "The map elements after 4th deletion are : \n";
      
        for (it1 = mp.begin(); it1!=mp.end(); ++it1)
            cout << it1->first << "->" << it1->second << endl;
          
        cout << endl;
          
    }
    

    输出:

    The initial map elements are : 
    a->5
    b->10
    c->15
    d->20
    e->30
    
    The map elements after 1st deletion are : 
    a->5
    c->15
    d->20
    e->30
    
    The map elements after 2nd deletion are : 
    a->5
    d->20
    e->30
    The number of elements deleted in 2nd deletion are : 1
    
    The map elements after 3rd deletion are : 
    a->5
    d->20
    e->30
    The number of elements deleted in 3rd deletion are : 0
    
    The map elements after 4th deletion are : 
    a->5
    
  2. 使用clear() :此函数清除地图中存在的所有元素。调用此函数后,映射的大小变为0。
    // C++ code to demonstrate the working of clear()
      
    #include
    #include // for map operations
    using namespace std;
      
    int main()
    {
        // declaring map
        // of char and int
        map< char, int > mp;
          
        // declaring iterator
        map::iterator it ;
          
        // inserting values 
         mp['a']=5;
         mp['b']=10;
         mp['c']=15;
         mp['d']=20;
         mp['e']=30;
      
        // printing initial map elements
        cout << "The initial map elements are : \n";
        for (it1 = mp.begin(); it1!=mp.end();  ++it1)
        cout << it1->first << "->" << it1->second << endl;
          
        // using clear() to erase all elements in map
        mp.clear();
          
        // printing map elements after deletion
        cout << "The map elements after clearing all elements are : \n";
        for (it1 = mp.begin(); it1!=mp.end();  ++it1)
        cout << it1->first << "->" << it1->second << endl;
          
    }
    

    输出:

    The initial map elements are : 
    a->5
    b->10
    c->15
    d->20
    e->30
    The map elements after clearing all elements are : 
    
要从最佳影片策划和实践问题去学习,检查了C++基础课程为基础,以先进的C++和C++ STL课程基础加上STL。要完成从学习语言到DS Algo等的更多准备工作,请参阅“完整面试准备课程”