C++教程
C++控制语句
C++函数
C++数组
C++指针
C++对象
C++继承
C++多态
C++抽象
C++常用
C++ STL教程
C++迭代器
C++程序

C++ multiset erase()

C++ multiset erase()

C++ multiset 擦除(strong)erase()函数用于删除与给定键关联的单个元素或元素范围([first,last))。因此,将通过删除元素的数量来减小大小。

语法

void erase (iterator position);                           //until C++ 11
size_type erase (const value_type& val);              //until C++ 11
void erase (iterator first, iterator last);           //until C++ 11
iterator  erase (const_iterator position);        //since C++ 11
size_type erase (const value_type& val);          //since C++ 11    
iterator  erase (const_iterator first, const_iterator last); //since C++ 11

参数

position: 指向要从容器中删除的元素的迭代器。
val : 要从 multiset 中删除的值。
first: 要擦除的范围的开始。
last: 要擦除的范围的结束。

返回值

擦除()函数返回指向已删除元素的下一个元素或返回已删除元素的数量的迭代器。

复杂度

erase(position): 摊销常数。
erase(value): 容器大小的对数。
erase(first,last):

迭代器有效性

迭代器,引用和指向该函数删除的元素的指针均无效。所有其他迭代器,指针和引用保持其有效性。

数据竞争

容器已修改。
删除的元素已修改。尽管同时访问其他元素是安全的,但在容器中进行迭代范围并不安全。

异常安全性

此函数不会引发异常。
如果指定了无效的范围或位置,则会导致未定义的行为。

示例1

让我们看一个简单的示例,该示例通过迭代器擦除元素。
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  multiset<int> mymultiset;
  multiset<int>::iterator it;
  mymultiset = {10,20,30,20};
  
  cout<<"Before erasing the element: \n";
   for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it << '\n';
  it=mymultiset.find('b');
  mymultiset.erase (*it);                   // erasing by iterator
  cout<<"\nAfter erasing the element: \n";
  for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it << '\n';
    
  return 0;
}
输出:
Before erasing the element: 
10
20
20
30
After erasing the element: 
10
20
20
30
在上面的示例中,元素被迭代器" it"擦除。

示例2

让我们看一个简单的示例来擦除元素具有给定键值的 multiset :
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  multiset<int> mymultiset;
  multiset<int>::iterator it;
  mymultiset = {10, 20, 30, 40, 10, 30};
  
  cout<<"Before erasing the element: \n";
   for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';
   mymultiset.erase (30);                  // erasing by value
  cout<<"\nAfter erasing the element: \n";
  for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
    cout << *it<< '\n';
  return 0;
}
输出:
Before erasing the element: 
10
10
20
30
30
40
After erasing the element: 
10
10
20
40
在上面的示例中,擦除(值)函数使用 multiset 中的值30。

示例3

让我们看一个简单的示例来擦除给定范围内的元素:
#include <iostream>
#include <set>
using namespace std;
int main ()
{
  multiset<int> mymultiset;
  multiset<int>::iterator it;
  mymultiset = {10, 20, 30, 20};
  
  cout<<"Before erasing the element are: \n";
   cout<<"Size is: "<<mymultiset.size()<<'\n';
   for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
   cout << *it << '\n';
   mymultiset.erase ( mymultiset.begin () ,  mymultiset.end () );   // erasing by range
  cout<<"\nAfter erasing the element are: \n";
  cout<<"Size is: "<<mymultiset.size();
  for (it=mymultiset.begin(); it!=mymultiset.end(); ++it)
  cout << *it << '\n';
  return 0;
}
输出:
Before erasing the element are: 
Size is: 3
10
20
30
After erasing the element are: 
Size is: 0
在上面的示例中,使用擦除(第一个,最后一个)函数擦除具有给定范围(即开始到结束)的元素。

示例4

让我们看一个简单的示例,从 multiset 中删除所有奇数:
#include <set>
#include <iostream>
using namespace std;
int main()
{
    multiset<int> m = {1,2,3,4,5,6,7,8,9,10,11,12,11,10,4};
                          
    // erase all odd numbers from m
    cout<<"After erasing odd numbers,elements are:\n ";
    for(auto it = m.begin(); it != m.end(); )
        if(*it % 2 == 1)
            it = m.erase(it);
        else
            ++it;
    for(auto& p : m)
        cout << p << ", ";
}
输出:
After erasing odd numbers, elements are:
 2, 4, 4, 6, 8, 10, 10, 12,
在上面的示例中,所有奇数均已删除并显示偶数。

示例5

让我们看看另一个示例:
#include <set>  
#include <string>  
#include <iostream>  
#include <iterator> // next() and prev() helper functions  
  
using namespace std;  
  
using mymultiset = multiset<string>;  
  
void printmultiset(const mymultiset& s) {  
    for (const auto& iter : s) {  
        cout << " [" << iter << "]";  
    }  
    cout << endl << "size() == " << s.size() << endl << endl;  
}  
  
int main()  
{  
    mymultiset s1;  
  
    // Fill in some data to test with, one at a time  
    s1.insert("Bob");  
    s1.insert("Robert");  
    s1.insert("Rob");  
    s1.insert("Rob");  
    s1.insert("Bob");  
  
    cout << "Starting data of multiset s1 is:" << endl;  
    printmultiset(s1);  
    // The 1st member function removes an element at a given position  
    s1.erase(next(s1.begin()));  
    cout << "After the 2nd element is deleted, the multiset s1 is:" << endl;  
    printmultiset(s1);  
  
    // Fill in some data to test with, one at a time, using an intializer list  
    mymultiset s2{ "meow", "nikita", "nikita", "growl", "yellow" };  
  
    cout << "Starting data of multiset s2 is:" << endl;  
    printmultiset(s2);  
    // The 2nd member function removes elements  
    // in the range [First, Last)  
    s2.erase(next(s2.begin()), prev(s2.end()));  
    cout << "After the middle elements are deleted, the multiset s2 is:" << endl;  
    printmultiset(s2);  
  
    mymultiset s3;  
  
    // Fill in some data to test with, one at a time, using emplace  
    s3.emplace("C");  
    s3.emplace("C#"); 
    s3.emplace("D");  
    s3.emplace("D#");  
    s3.emplace("E");  
    s3.emplace("E#");  
    s3.emplace("F");  
    s3.emplace("F#");  
    s3.emplace("G");  
    s3.emplace("G#");  
    s3.emplace("E");  
    s3.emplace("E#"); 
    s3.emplace("B");  
  
    cout << "Starting data of multiset s3 is:" << endl;  
    printmultiset(s3);  
    // The 3rd member function removes elements with a given Key  
    mymultiset::size_type count = s3.erase("E#");  
    // The 3rd member function also returns the number of elements removed  
    cout << "The number of elements removed from s3 is: " << count << "." << endl;  
    cout << "After the element with a key of \"E#\" is deleted, the multiset s3 is:" << endl;  
    printmultiset(s3);  
}  
输出:
Starting data of multiset s1 is:
 [Bob] [Bob] [Rob] [Rob] [Robert]
size() == 5
After the 2nd element is deleted, the multiset s1 is:
 [Bob] [Rob] [Rob] [Robert]
size() == 4
Starting data of multiset s2 is:
 [growl] [meow] [nikita] [nikita] [yellow]
size() == 5
After the middle elements are deleted, the multiset s2 is:
 [growl] [yellow]
size() == 2
Starting data of multiset s3 is:
 [B] [C] [C#] [D] [D#] [E] [E] [E#] [E#] [F] [F#] [G] [G#]
size() == 13
The number of elements removed from s3 is: 2.
After the element with a key of "E#" is deleted, the multiset s3 is:
 [B] [C] [C#] [D] [D#] [E] [E] [F] [F#] [G] [G#]
size() == 11
昵称: 邮箱:
Copyright © 2022 立地货 All Rights Reserved.
备案号:京ICP备14037608号-4