C++ STL中的multiset的clear()函数
multiset::clear() 函数是C++ STL中的内置函数,它从multiset容器中删除所有元素。删除后,multiset容器的最终大小为0。
语法:
multiset_name.clear()
参数: 该函数不接受任何参数。
返回值: 该函数不返回任何内容。
下面的程序阐述了multiset::clear()函数:
程序1:
// C++ 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;
}
输出:
multiset中的元素为: 10 10 11 15 15
clear()后的大小为: 0
时间复杂度: O(N)
程序2:
// C++ 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;
}
输出:
multiset中的元素为: 10 10 11 15 15 18 18 20 20
clear()后的大小为: 0
时间复杂度: O(N)