多集是一种 关联容器 与集合类似,只是多个元素可以具有相同的值。 与multiset相关的一些基本功能:
null
- 开始 –将迭代器返回到multiset中的第一个元素。
- 结束() –将迭代器返回到多集合中最后一个元素后面的理论元素。
- 大小() –返回多集合中的元素数。
- 最大尺寸() –返回multiset可以容纳的最大元素数。
- 空的() –返回多集是否为空
实施:
CPP
// CPP Program to demonstrate the // implementation of multiset #include <iostream> #include <iterator> #include <set> using namespace std; int main() { // empty multiset container multiset< int , greater< int > > gquiz1; // insert elements in random order gquiz1.insert(40); gquiz1.insert(30); gquiz1.insert(60); gquiz1.insert(20); gquiz1.insert(50); // 50 will be added again to // the multiset unlike set gquiz1.insert(50); gquiz1.insert(10); // printing multiset gquiz1 multiset< int , greater< int > >::iterator itr; cout << "The multiset gquiz1 is : " ; for (itr = gquiz1.begin(); itr != gquiz1.end(); ++itr) { cout << *itr << " " ; } cout << endl; // assigning the elements from gquiz1 to gquiz2 multiset< int > gquiz2(gquiz1.begin(), gquiz1.end()); // print all elements of the multiset gquiz2 cout << "The multiset gquiz2 " "after assign from gquiz1 is : " ; for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) { cout << *itr << " " ; } cout << endl; // remove all elements up to element // with value 30 in gquiz2 cout << "gquiz2 after removal " "of elements less than 30 : " ; gquiz2.erase(gquiz2.begin(), gquiz2.find(30)); for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) { cout << *itr << " " ; } // remove all elements with value 50 in gquiz2 int num; num = gquiz2.erase(50); cout << "gquiz2.erase(50) : " ; cout << num << " removed " ; for (itr = gquiz2.begin(); itr != gquiz2.end(); ++itr) { cout << *itr << " " ; } cout << endl; // lower bound and upper bound for multiset gquiz1 cout << "gquiz1.lower_bound(40) : " << *gquiz1.lower_bound(40) << endl; cout << "gquiz1.upper_bound(40) : " << *gquiz1.upper_bound(40) << endl; // lower bound and upper bound for multiset gquiz2 cout << "gquiz2.lower_bound(40) : " << *gquiz2.lower_bound(40) << endl; cout << "gquiz2.upper_bound(40) : " << *gquiz2.upper_bound(40) << endl; return 0; } |
输出
The multiset gquiz1 is : 60 50 50 40 30 20 10 The multiset gquiz2 after assign from gquiz1 is : 10 20 30 40 50 50 60 gquiz2 after removal of elements less than 30 : 30 40 50 50 60 gquiz2.erase(50) : 2 removed 30 40 60 gquiz1.lower_bound(40) : 40gquiz1.upper_bound(40) : 30gquiz2.lower_bound(40) : 40gquiz2.upper_bound(40) : 60
从具有相同值的多集合中删除元素:
- a、 抹去 –从具有相同值的multiset中删除元素的所有实例
- a、 删除(a.查找()) –仅从具有相同值的multiset中删除元素的一个实例
C++
// CPP Code to remove an element from multiset which have // same value #include <bits/stdc++.h> using namespace std; int main() { multiset< int > a; a.insert(10); a.insert(10); a.insert(10); // it will give output 3 cout << a.count(10) << endl; // removing single instance from multiset // it will remove only one value of // 10 from multiset a.erase(a.find(10)); // it will give output 2 cout << a.count(10) << endl; // removing all instance of element from multiset // it will remove all instance of value 10 a.erase(10); // it will give output 0 because all // instance of value is removed from // multiset cout << a.count(10) << endl; return 0; } |
输出
320
多集函数列表
作用 |
释义 |
---|---|
开始 | 将迭代器返回到multiset中的第一个元素。 |
结束() | 将迭代器返回到多集合中最后一个元素后面的理论元素。 |
大小() | 返回多集合中的元素数。 |
最大尺寸() | 返回多集可以容纳的最大元素数。 |
空的() | 返回多集是否为空。 |
成对插入(常数g) | 将新元素“g”添加到多集。 |
迭代器插入(迭代器位置,常数g) | 在迭代器指向的位置添加新元素“g”。 |
擦除(迭代器位置) | 在迭代器指向的位置删除元素。 |
擦除(常数g) | 从多重集合中删除值“g”。 |
清除() | 从多重集中删除所有元素。 |
基尤公司()/ 价值_comp() | 返回确定多集合中元素排序方式的对象(默认情况下为“ |
查找(常数g) | 将迭代器返回到多集合中的元素“g”(如果找到),否则返回迭代器结束。 |
计数(常数g) | 返回多集合中元素“g”的匹配数。 |
下限(常数g) | 将迭代器返回到第一个元素,该元素相当于“g”,或者如果找到,肯定不会在多集合中的元素“g”之前,否则返回迭代器结束。 |
上界(常数g) | 返回一个迭代器到第一个元素,该元素相当于“g”,或者如果找到,肯定会在多集合中紧跟元素“g”,否则返回迭代器结束。 |
多集::交换() | 此函数用于交换两个多集合的内容,但集合的类型必须相同,尽管大小可能不同。 |
多集::运算符= | 此运算符用于通过替换现有内容将新内容分配给容器。 |
multiset::emplace() | 此函数用于将新元素插入到multiset容器中。 |
多集等_范围() | 返回成对的迭代器。该对指的是包含容器中所有元素的范围,这些元素的密钥相当于k。 |
multiset::emplace_hint() | 在多重集中插入新元素。 |
多集::rbegin() | 返回指向multiset容器中最后一个元素的反向迭代器。 |
多集::rend() | 返回一个反向迭代器,该迭代器指向multiset容器中第一个元素之前的理论元素。 |
多集::cbegin() | 返回指向容器中第一个元素的常量迭代器。 |
多集::cend() | 返回一个常量迭代器,该迭代器指向容器中最后一个元素之后的位置。 |
多集::crbegin() | 返回指向容器中最后一个元素的常量反向迭代器。 |
多集::crend() | 返回一个常量反向迭代器,该迭代器指向容器中第一个元素之前的位置。 |
multiset::get_分配器() | 返回与multiset关联的分配器对象的副本。 |
如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。
© 版权声明
文章版权归作者所有,未经允许请勿转载。
THE END