当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


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。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。