在本文中,我们将讨论C ++ STL中multiset::clear()函数的工作原理,语法和示例。
多重集是类似于集合容器的容器,这意味着它们以键的形式(类似于集合)以特定顺序存储值。
在多集合中,将值标识为与集合相同的键。多重集和集合之间的主要区别在于,集合具有不同的键,这意味着没有两个键是相同的,在多重集中可以有相同的键值。
多集键用于实现二进制搜索树。
multiset::clear()函数是C ++ STL中的内置函数,在<set>头文件中定义。
这用于清除整个多集容器。
clear()从多集容器的元素中删除所有元素,并使多集容器的大小为0。
ms_name.clear();
该函数不接受任何参数。
此函数不返回任何内容
Input: std::multiset<int> mymultiset = {1, 2, 2, 3, 4}; mymultiset.clear(); mymultiset.size(); Output: size of multiset = 0
#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {2, 4, 1, 3, 8, 5, 6}; multiset<int> check(arr, arr + 7); cout<<"List is : "; for (auto i = check.begin(); i != check.end(); i++) cout << *i << " "; cout<<"\nList when clear() is applied: "; check.clear(); for (auto i = check.begin(); i != check.end(); i++) cout << *i << " "; return 0; }
输出结果
如果我们运行上面的代码,它将生成以下输出-
List is : 1 2 3 4 5 6 8 List when clear() is applied:
#include <bits/stdc++.h> using namespace std; int main() { int arr[] = {2, 4, 1, 3, 8, 5, 6}; multiset<int> check(arr, arr + 7); cout<<"List is : "; for (auto i = check.begin(); i != check.end(); i++) cout << *i << " "; cout<<"\nList when clear() is applied: "; if(check.empty()) { cout<<"\nList is null"; } else { cout<<"\nList isn't null : "; for (auto i = check.begin(); i != check.end(); i++) cout << *i << " "; cout<<"\nsize is : "<<check.size(); } int arr2[] = {2, 4, 1, 3, 8, 5, 6}; multiset<int> check_2(arr2, arr2 + 7); cout<<"\nList when clear() is applied: "; check_2.clear(); if(check_2.empty()) { cout<<"\nList is null"; cout<<"\nsize is : "<<check_2.size(); } else { cout<<"\nList isn't null : "<<check_2.size(); for (auto i = check_2.begin(); i != check_2.end(); i++) cout << *i << " "; cout<<"\nsize is : "<<check_2.size(); } return 0; }
输出结果
如果我们运行上面的代码,它将生成以下输出-
List is : 1 2 3 4 5 6 8 List when clear() is applied: List isn't null : 1 2 3 4 5 6 8 Size is : 7 List when clear() is applied: List is null size is : 0