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
相关用法
- C++ multiset insert()用法及代码示例
- C++ multiset find()用法及代码示例
- C++ multiset equal_range()用法及代码示例
- C++ multiset get_allocator()用法及代码示例
- C++ multiset begin()、end()用法及代码示例
- C++ multiset empty()用法及代码示例
- C++ multiset emplace_hint()用法及代码示例
- C++ multiset key_comp()用法及代码示例
- C++ multiset count()用法及代码示例
- C++ multiset cbegin()、cend()用法及代码示例
- C++ multiset rbegin()、rend()用法及代码示例
- C++ multiset crbegin()、crend()用法及代码示例
- C++ unoredered_set clear()用法及代码示例
- C++ ios clear()用法及代码示例
- C++ unordered_multiset clear()用法及代码示例
注:本文由纯净天空筛选整理自gopaldave大神的英文原创作品 multiset clear() function in C++ STL。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。