當前位置: 首頁>>代碼示例 >>用法及示例精選 >>正文


C++ set erase()用法及代碼示例


C++ set 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:iterator 指向要從集合中刪除的單個元素。

val:要從集合中刪除的值。

first:要擦除的範圍的開始。

last:要擦除的範圍結束。

返回值

它返回一個指向已刪除元素的下一個元素的迭代器或返回已刪除元素的數量。

複雜度

擦除(位置):攤銷常數。

擦除(val):容器大小的對數。

擦除(第一個,最後一個):第一個和最後一個之間的距離是線性的。

迭代器有效性

迭代器、引用和指向被函數刪除的元素的指針無效。

所有其他迭代器、指針和引用保持其有效性。

數據競爭

容器被修改。

刪除的元素被修改。盡管同時訪問其他元素是安全的,但在容器中迭代範圍並不安全。

異常安全

該函數不會拋出異常。

如果指定了無效的範圍或位置,則會導致未定義的行為。

例子1

讓我們看看通過迭代器擦除元素的簡單示例。

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  myset = {10,20,30};
  
  cout<<"Before erasing the element:\n";
   for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it << '\n';

  it=myset.find('b');
  myset.erase (*it);                   // erasing by iterator

  cout<<"\nAfter erasing the element:\n";
  for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it << '\n';
    
  return 0;
}

輸出:

Before erasing the element:
10
20
30

After erasing the element:
10
20
30

在上麵的例子中,元素被迭代器 it 擦除。

例子2

讓我們看一個簡單的例子,用給定的鍵值擦除集合的元素:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  myset = {10, 20, 30, 40};
  
  cout<<"Before erasing the element:\n";
   for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it<< '\n';

   myset.erase (30);                  // erasing by value

  cout<<"\nAfter erasing the element:\n";
  for (it=myset.begin(); it!=myset.end(); ++it)
    cout << *it<< '\n';
  return 0;
}

輸出:

Before erasing the element:
10
20
30
40

After erasing the element:
10
20
40

在上麵的例子中,erase(value) 函數使用集合中的值 30。

例子3

讓我們看一個簡單的例子來擦除給定範圍內的元素:

#include <iostream>
#include <set>

using namespace std;

int main ()
{
  set<int> myset;
  set<int>::iterator it;

  myset = {10, 20, 30};
  
  cout<<"Before erasing the element are:\n";
   cout<<"Size is:"<<myset.size()<<'\n';
   for (it=myset.begin(); it!=myset.end(); ++it)
   cout << *it << '\n';

   myset.erase ( myset.begin () ,  myset.end () );   // erasing by range

  cout<<"\nAfter erasing the element are:\n";
  cout<<"Size is:"<<myset.size();
  for (it=myset.begin(); it!=myset.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

在上麵的例子中,erase(first, last) 函數用於擦除給定範圍內的元素,即開始到結束。

示例 4

讓我們看一個簡單的例子來擦除集合中的所有奇數:

#include <set>
#include <iostream>

using namespace std;

int main()
{
    set<int> m = {1,2,3,4,5,6,7,8,9,10,11,12,13,14,15};
                          
    // 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, 6, 8, 10, 12, 14,

在上麵的例子中,所有的奇數都被擦除了,顯示的是偶數。

例 5

讓我們看另一個例子:

#include <set>  
#include <string>  
#include <iostream>  
#include <iterator> // next() and prev() helper functions  
  
using namespace std;  
  
using myset = set<string>;  
  
void printset(const myset& s) {  
    for (const auto& iter:s) {  
        cout << " [" << iter << "]";  
    }  
    cout << endl << "size() == " << s.size() << endl << endl;  
}  
  
int main()  
{  
    myset s1;  
  
    // Fill in some data to test with, one at a time  
    s1.insert("Bob");  
    s1.insert("Robert");  
    s1.insert("Bert");  
    s1.insert("Rob");  
    s1.insert("Bobby");  
  
    cout << "Starting data of set s1 is:" << endl;  
    printset(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 set s1 is:" << endl;  
    printset(s1);  
  
    // Fill in some data to test with, one at a time, using an intializer list  
    myset s2{ "meow", "hiss", "purr", "growl", "yowl" };  
  
    cout << "Starting data of set s2 is:" << endl;  
    printset(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 set s2 is:" << endl;  
    printset(s2);  
  
    myset 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("A");  
    s3.emplace("A#");  
    s3.emplace("B");  
  
    cout << "Starting data of set s3 is:" << endl;  
    printset(s3);  
    // The 3rd member function removes elements with a given Key  
    myset::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 set s3 is:" << endl;  
    printset(s3);  
}

輸出:

Starting data of set s1 is:
 [Bert] [Bob] [Bobby] [Rob] [Robert]
size() == 5

After the 2nd element is deleted, the set s1 is:
 [Bert] [Bobby] [Rob] [Robert]
size() == 4

Starting data of set s2 is:
 [growl] [hiss] [meow] [purr] [yowl]
size() == 5

After the middle elements are deleted, the set s2 is:
 [growl] [yowl]
size() == 2

Starting data of set s3 is:
 [A] [A#] [B] [C] [C#] [D] [D#] [E] [E#] [F] [F#] [G] [G#]
size() == 13

The number of elements removed from s3 is:1.
After the element with a key of "E#" is deleted, the set s3 is:
 [A] [A#] [B] [C] [C#] [D] [D#] [E] [F] [F#] [G] [G#]
size() == 12





相關用法


注:本文由純淨天空篩選整理自 C++ set erase()。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。