码迷,mamicode.com
首页 > 其他好文 > 详细

stl之multiset容器的应用

时间:2017-07-18 19:43:21      阅读:138      评论:0      收藏:0      [点我收藏+]

标签:内存   multiset   sdn   管理   span   pac   nbsp   ret   不同   

 与set集合容器一样,multiset多重集合容器也使用红黑树组织元素数据,仅仅是multiset容器同意将反复的元素健值插入。而set容器则不同意。

set容器所使用的C++标准头文件set。事实上也是multiset容器的头文件。由于这个set头文件也包括multiset所需的红黑树和自身实现文件。仅仅要用宏语句“#include<set>”包括进来,就可对multiset容器的应用代码进行编译。

创建multiset对象

与set容器一样,multiset容器提供例如以下构造函数。创建multiset对象来管理内部红黑树中的节点元素数据。

1.  set(); 用默认的 less<T>函数对象和内存分配器,创建一个没有不论什么数据元素的 set对象。

2.  set(constkey_compare& comp); 指定一个比較函数对象comp 来创建 set 对象,内存分配器为默认值。

3.  set(constset&);  set拷贝构造函数。通过红黑树的拷贝构造函数。实现两个set容器的元素、头节点和节点个数的拷贝。

4.  set(InputIteratorfirst, InputIteratorlast); 用迭代器区间 [first, last)所指的元素。创建一个 set对象。

5.  set(InputIteratorfirst,InputIterator last, const key_compare& comp);//用迭代器区间 [first, last)所指的元素和comp函数对象,创建一个 set对象。

#include <iostream>
#include <set>
using namespace std;

bool fncomp (int lhs, int rhs) {return lhs<rhs;}

struct classcomp 
{
	bool operator() (const int& lhs, const int& rhs) const
	{return lhs<rhs;}
};
//5种创建multiset对象的方式
int main ()
{
	multiset<int> first;                         
	int myints[]= {10,20,30,20,20};
	multiset<int> second (myints,myints+5);      
	multiset<int> third (second);                
	multiset<int> fourth (second.begin(), second.end()); 
	multiset<int,classcomp> fifth;              
	return 0;
}

元素的插入和删除及搜索

multiset容器元素的插入和删除、搜索与set容器一致,详细能够參考上篇set容器的应用

其它函数

count(); 返回指向某个值元素的个数

#include <iostream>
#include <set>
using namespace std;
int main ()
{
       intmyints[]={10,73,12,22,73,73,12};
       multiset<int>mymultiset (myints,myints+7);
       cout<< "73 appears " << mymultiset.count(73) << "times in mymultiset.\n";
       return0;
}

技术分享

empty(); 假设集合为空,返回true

equal_range(); 返回集合中与给定值相等的上下限的两个迭代器

find(); 返回一个指向被查找到元素的迭代器

get_allocator(); 返回多元集合的分配器

#include <iostream>
#include <set>
using namespace std;
int main ()
{
       multiset<int>mymultiset;
       int* p;
       unsignedint i;
       //用get_allocator申请含义个元素的内存空间
       p=mymultiset.get_allocator().allocate(5);
       //对内存空间进行赋值
       for(i=0; i<5; i++) p[i]=(i+1)*10;
       cout<< "所申请的数组空间包括元素::";
       for(i=0; i<5; i++)
              cout<< ' ' << p[i];
       cout<< '\n';
       //施放内存空间
       mymultiset.get_allocator().deallocate(p,5);
       return0;
}
技术分享

key_comp(); 返回一个用于元素间值比較的函数。默认<

#include <iostream>
#include <set>
using namespace std;
int main ()
{
       multiset<int>mymultiset;
       for(int i=0; i<5; i++)
           mymultiset.insert(i);
       multiset<int>::key_comparemycomp = mymultiset.key_comp();
       cout<< "mymultiset contains:";
       inthighest = *mymultiset.rbegin();
       multiset<int>::iteratorit = mymultiset.begin();
       do{
              std::cout<< ' ' << *it;
       }while ( mycomp(*it++,highest) );
       cout<< '\n';
       return0;
}
技术分享
lower_bound(); 返回指向大于(或等于)某值的第一个元素的迭代器

max_size(); 返回集合能容纳的元素的最大限值

size(); 多元集合中元素的数目

swap(); 交换两个多元集合变量

upper_bound(); 返回一个大于某个值元素的迭代器

value_comp(); 返回一个用于比較元素间的值的函数

转载请注明出处:http://blog.csdn.net/lsh_2013/article/details/46754979,谢谢合作!


stl之multiset容器的应用

标签:内存   multiset   sdn   管理   span   pac   nbsp   ret   不同   

原文地址:http://www.cnblogs.com/yfceshi/p/7202157.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!