日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

STL set和multiset

發布時間:2023/12/20 编程问答 30 豆豆
生活随笔 收集整理的這篇文章主要介紹了 STL set和multiset 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

***************************************************

更多精彩,歡迎進入:http://shop115376623.taobao.com

STL視頻教程:

http://item.taobao.com/item.htm?spm=a1z10.5-c.w4002-9510581626.21.y9vLuz&id=43055362725

***************************************************


C++ STL set和multiset的使用



1,set的含義是集合,它是一個有序的容器,里面的元素都是排序好的,支持插入,刪除,查找等操作,就 ? ?像一個集合一樣。所有的操作的都是嚴格在logn時間之內完成,效率非常高。?

set和multiset的區別是:set:插入的元素不能相同,但是multiset可以相同。



? ?創建 multiset<ss> base;


? ?刪除:如果刪除元素a,那么在定義的比較關系下和a相等的所有元素都會被刪除


? ?base.count( a ):set能返回0或者1,multiset是有多少個返回多少個.


? ?Set和multiset都是引用<set>頭文件,復雜度都是logn


2,Set中的元素可以是任意類型的,但是由于需要排序,所以元素必須有一個序,即大小的比較關系,比如 ? ?整數可以用<比較.


3,自定義比較函數;


? ? include<set>


? ? typedef struct


? ? { 定義類型 }


? ? ss(類型名);


? ? struct cmp


? ? {


? ? ? ? ? bool operator()( const int &a, const int &b ) const


? ? ? ? ? ? ?{ 定義比較關系<}


? ? };


? ? (運算符重載,重載<)


? ? set<ss> base; ( 創建一個元素類型是ss,名字是base的set )


? ? 注:定義了<,==和>以及>=,<=就都確定了,STL的比較關系都是用<來確定的,所以必須通 ? ? 過定義< --“嚴格弱小于”來確定比較關


4,set的基本操作:


begin() ? ? ? ? 返回指向第一個元素的迭代器


clear() ? ? ? ? 清除所有元素


count() ? ? ? ? 返回某個值元素的個數


empty() ? ? ? ? 如果集合為空,返回true


end() ? ? ? ? ? 返回指向最后一個元素的迭代器


equal_range() ? 返回集合中與給定值相等的上下限的兩個迭代器


erase() ? ? ? ? 刪除集合中的元素


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


get_allocator() 返回集合的分配器


insert() ? ? ? ?在集合中插入元素


lower_bound() ? 返回指向大于(或等于)某值的第一個元素的迭代器


key_comp() ? ? ?返回一個用于元素間值比較的函數


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


rbegin() ? ? ? ?返回指向集合中最后一個元素的反向迭代器


rend() ? ? ? ? ?返回指向集合中第一個元素的反向迭代器


size() ? ? ? ? ?集合中元素的數目


swap() ? ? ? ? ?交換兩個集合變量


upper_bound() ? 返回大于某個值元素的迭代器


value_comp() ? ?返回一個用于比較元素間的值的函數


5,自定義比較函數:


For example:


#include<iostream>


#include<set>


using namespace std;


typedef struct {


int a,b;


char s;


}newtype;


struct compare ? //there is no ().


{


bool operator()(const newtype &a, const newtype &b) const


{


return a.s<b.s;


}


};//the “; ” ?is ?here;


set<newtype,compare>element;


int main()


{


newtype a,b,c,d,t;


a.a=1; a.s='b';


b.a=2; b.s='c';


c.a=4; c.s='d';


d.a=3; d.s='a';


element.insert(a);


element.insert(b);


element.insert(c);


element.insert(d);


set<newtype,compare>::iterator it;


for(it=element.begin(); it!=element.end();it++)


cout<<(*it).a<<" ";


cout<<endl;


for(it=element.begin(); it!=element.end();it++)


cout<<(*it).s<<" ";


}


element自動排序是按照char s的大小排序的;


6.其他的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;}


};


int main ()


{


? set<int> first; ? ? ? ? ? ? ? ? ? ? ? ? ? // empty set of ints


? int myints[]= {10,20,30,40,50};


? set<int> second (myints,myints+5); ? ? ? ?// pointers used as iterators


? set<int> third (second); ? ? ? ? ? ? ? ? ?// a copy of second


? set<int> fourth (second.begin(), second.end()); ?// iterator ctor.


? set<int,classcomp> fifth; ? ? ? ? ? ? ? ? // class as Compare


? bool(*fn_pt)(int,int) = fncomp;


? set<int,bool(*)(int,int)> sixth (fn_pt); ?// function pointer as Compare


?


? return 0;


}

總結

以上是生活随笔為你收集整理的STL set和multiset的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。