1. 程式人生 > >stl之multiset容器的應用

stl之multiset容器的應用

fin true detail 多重 tar ret 代碼 oca 頭節點

與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容器的應用