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


C++ multiset clear()用法及代碼示例


multiset::clear(函數是C++ STL中的內置函數,可從多集容器中刪除所有元素。刪除後多圖容器的最終大小為0。

用法:

multiset_name.clear()

參數:該函數不接受任何參數。


返回值:該函數不返回任何內容。

以下示例程序旨在說明multimap::clear()函數:

示例1:

// CPP program to demonstrate the 
// multiset::clear() function 
#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
  
    int arr[] = { 15, 10, 15, 11, 10 }; 
  
    // initializes the set from an array 
    multiset<int> s(arr, arr + 5); 
  
    // prints all elements in set 
    cout << "The elements in multiset are: "; 
    for (auto it = s.begin(); it != s.end(); it++) 
        cout << *it << " "; 
  
    cout << "\nThe size after clear() is: "; 
  
    // erases all elements 
    s.clear(); 
    cout << s.size(); 
  
    return 0; 
}
輸出:
The elements in multiset are: 10 10 11 15 15 
The size after clear() is: 0

示例2:

// CPP program to demonstrate the 
// multiset::clear() function 
#include <bits/stdc++.h> 
using namespace std; 
int main() 
{ 
  
    int arr[] = { 15, 10, 15, 11, 10, 18, 18, 20, 20 }; 
  
    // initializes the set from an array 
    multiset<int> s(arr, arr + 9); 
  
    // prints all elements in set 
    cout << "The elements in multiset are: "; 
    for (auto it = s.begin(); it != s.end(); it++) 
        cout << *it << " "; 
  
    cout << "\nThe size after clear() is: "; 
  
    // erases all elements 
    s.clear(); 
    cout << s.size(); 
  
    return 0; 
}
輸出:
The elements in multiset are: 10 10 11 15 15 18 18 20 20 
The size after clear() is: 0


相關用法


注:本文由純淨天空篩選整理自gopaldave大神的英文原創作品 multiset clear() function in C++ STL。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。