C++ multimap clear()函数
C++ multimap clear() 函数用于删除multimap容器的所有元素。它清空multimap并将其大小设置为0。
语法
void clear(); //until C++ 11
void clear() noexcept; //since C++ 11
参数
无
返回值
无
复杂度
与大小成线性关系。
迭代器有效性
与该容器相关的所有迭代器、引用和指针都将失效。
数据竞争
该容器被修改。
所有包含的元素都被修改。
异常安全性
此函数永远不会抛出异常。
示例1
让我们看一个简单的示例,在清空操作之前和之后计算multimap的大小:
#include <iostream>
#include <map>
using namespace std;
int main() {
multimap<char, int> mymultimap = {
{'a', 1},
{'b', 2},
{'b', 3},
{'d', 4},
{'d', 5},
};
cout << "Initial size of multimap before clear operation = " << mymultimap.size() << endl;
mymultimap.clear();
cout << "Size of multimap after clear operation = " << mymultimap.size() << endl;
return 0;
}
输出:
Initial size of multimap before clear operation = 5
Size of multimap after clear operation = 0
在上面的示例中,一个multimap被初始化为5个元素,因此大小为5,但在clear操作之后,大小变为0。
示例2
让我们看一个简单的示例来清除multimap的元素:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
multimap<int,string> mymultimap;
mymultimap = {
{1, "Nikita"},
{2, "Divya" },
{1, "Ashish"} };
cout << "mymultimap contains:\n";
for (multimap<int,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " : " << it->second << '\n';
mymultimap.clear();
mymultimap = {
{1, "Aman"},
{2, "Deep"}
};
cout << "\nmymultimap contains:\n";
for (multimap<int,string>::iterator it=mymultimap.begin(); it!=mymultimap.end(); ++it)
cout << it->first << " : " << it->second << '\n';
return 0;
}
输出:
mymultimap contains:
1 : Nikita
1 : Ashish
2 : Divya
mymultimap contains:
1 : Aman
2 : Deep
在上面的示例中,清除了multimap后,我们可以在不初始化的情况下添加新元素。
示例3
让我们看一个简单的示例来清除multimap的元素:
#include <iostream>
#include <map>
using namespace std;
int main ()
{
int n;
multimap<int,string> m1,m2,m3;
m1 = { {1, "Nikita"},
{2, "Deep"},
{1, "Ashish"} };
m2 = { {1, "Nidhi"},
{2, "Priya"},
{2, "Gitanjali"} };
m3 = { {1, "Manas"},
{3, "Fruti"},
{3, "Kamlesh"} };
cout << "m1 group has following members:\n";
for (multimap<int,string>::iterator it=m1.begin(); it!=m1.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout << "m2 group has following members:\n";
for (multimap<int,string>::iterator it=m2.begin(); it!=m2.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout << "m3 group has following members:\n";
for (multimap<int,string>::iterator it=m3.begin(); it!=m3.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout<<"\nWhich group do you want to delete?\n 1.m1\n 2.m2\n 3.m3\n Please enter your choice: ";
cin>>n;
if(n==1){
m1.clear();
cout<<"\nGroup m1 has been cleared.";
}
else if(n==2){
m2.clear();
cout<<"\nGroup m2 has been cleared.";
}
else if(n==3){
m3.clear();
cout<<"\nGroup m3 has been cleared.";
}
else
cout<<"Invalid option!";
return 0;
}
输出:
m1 group has following members:
1 : Nikita
1 : Ashish
2 : Deep
m2 group has following members:
1 : Nidhi
2 : Priya
2 : Gitanjali
m3 group has following members:
1 : Manas
3 : Fruti
3 : Kamlesh
Which group do you want to delete?
1.m1
2.m2
3.m3
Please enter your choice: 2
Group m2 has been cleared.
在上面的示例中,有三组multimap,根据用户的选择删除了一组。
示例4
让我们看一个简单的示例:
#include <iostream>
#include <map>
using namespace std;
int main() {
int n;
multimap<string, int> fruit = {
{"Banana", 40},
{"Apple", 190},
{"Orange", 120},
};
cout << "Fruit bucket has following fruits = \n";
for (multimap<string,int>::iterator it=fruit.begin(); it!=fruit.end(); ++it)
cout << it->first << " : " << it->second << '\n';
cout<<"\nDo you want to clear your fruit bucket?\nPress 1 for Yes and 0 for No: ";
cin>>n;
if( n==1){
fruit.clear();
cout<<fruit.size()<<" fruits in bucket \n";
}
else if(n==0)
cout <<fruit.size() << " fruits in bucket \n" ;
return 0;
}
输出:
1.
Fruit bucket has following fruits =
Apple : 190
Banana : 40
Orange : 120
Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 0
3 fruits in bucket
2.
Fruit bucket has following fruits =
Apple : 190
Banana : 40
Orange : 120
Do you want to clear your fruit bucket?
Press 1 for Yes and 0 for No: 1
0 fruits in bucket
在上面的示例中,一个水果multimap被初始化为三个水果。如果你输入0来请求清除multimap,那么水果桶将拥有3个元素;或者如果你输入1,则会清空水果multimap,使其大小变为0。