Last Updated : 30 Nov, 2021
The multiset::clear() function is a built-in function in C++ STL which removes all elements from the multiset container. The final size of multiset container after removal is 0.
Syntax:
multiset_name.clear()
Parameters: The function does not accept any parameter.
Return Value: The function does not returns anything.
Below programs illustrates the multiset::clear() function:
Program 1:
C++
// 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;
}
The elements in multiset are: 10 10 11 15 15 The size after clear() is: 0
Time Complexity: O(N), where N is the total number of elements present in the multiset.
Auxiliary Space: O(N)
Program 2:
C++
// 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;
}
The elements in multiset are: 10 10 11 15 15 18 18 20 20 The size after clear() is: 0
Time Complexity: O(N), where N is the total number of elements present in the multiset.
Auxiliary Space: O(N)
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4