STL泛型编程-multiset多重集合容器
2013-12-15 17:47
267 查看
multiset多重集合容器
multiset与set一样,也是使用红黑树来组织元素数据的,唯一不同的是,multiset允许重复的元素键值插入,而set不允许。multiset也需要声明头文件"#include <set>",由于它包含重复元素,所以,在插入元素,删除元素、查找元素上较set有差别。
元素插入:
#include <set> #include <iostream> using namespace std; int main(int argc,char* argv[]) { multiset<string> ms; ms.insert("abc"); ms.insert("123"); ms.insert("111"); ms.insert("aaa"); s.insert("123"); multiset<string>::iterator it;//定义前向迭代器 for(it=ms.begin();it!=ms.end();it++) cout<<*it<<" "; cout<<endl; return 0; } //运行结果: //111 //123 //123 //aaa //abc
元素删除:返回删除元素的个数
#include <set> #include <iostream> using namespace std; int main(int argc,char* argv[]) { multiset<string> ms; ms.insert("abc"); ms.insert("123"); ms.insert("111"); ms.insert("aaa"); s.insert("123"); multiset<string>::iterator it;//定义前向迭代器 for(it=ms.begin();it!=ms.end();it++) cout<<*it<<endl; //删除值为"123"的所有重复元素,返回删除元素总数2 int n=ms.erase("123"); cout<<"Total deleted : "<<n<<endl; //输出删除后的剩余元素 cout<<"all elements after deleted : "<<endl; for(it=ms.begin();it!=ms.end();it++) cout<<*it<<endl; return 0; } //运行结果: //111 //123 //123 //aaa //abc //Total deleted : 2 //all element after deleted : //111 //aaa //abc
查找元素及其他,与set相似
相关文章推荐
- STL学习笔记-set/multiset容器(集合)
- STL泛型编程-set集合容器
- C++ STL学习笔记八 multiset多重集合容器
- 容器stl——multiset(集合中可以有重复的值) ny55懒省事的小明
- C++STL之multiset多重集合容器
- 【C++】STL常用容器总结之十:多重映射multimap和多重集合multiset
- 16 STL集合和映射容器set/multiset_map/multimap
- C++ STL学习笔记八 multiset多重集合容器
- STL(十二)multiset多重集合容器
- C++/STL关联容器-集合-multiset&set的用法
- STL容器:set/map/multiset/multimap 与hash_set/hash_map/hash_multiset/hash_multimap
- STL中的set集合容器进行集合运算:并、交、差实例
- STL中的set集合容器进行集合运算:并、交、差实例
- STL的基本使用之关联容器:set和multiSet的基本使用
- STL学习笔记----5.容器 set 和 multiset
- 有关STL中的容器和MFC的集合类型构造函数区别的一点思考
- [STL基础]multiset多重集合
- STL之set集合容器
- Multiset多重集合容器
- STL容器使用DEMO-multiset