sets是乙個儲存元素的容器,其中每個元素最多隻出現一次,元素的遵循乙個特定的順序。元素一旦被放入次容器將不能被修改(修改元素的值),但是可以對元素進行插入和移除操作。set內部的元素遵循嚴格弱排序,因此在尋找元素的時候比unordered_map稍微慢一些,但是可以直接通過指標操作子分組,set是乙個典型的二分搜尋樹的應用。
定義程式
#include
#include
bool fncomp (int lhs, int rhs) ;
std::set
second (myints,myints+5); // 範圍
std::set
third (second); // 複製second
std::set
fourth (second.begin(), second.end()); // iterator ctor.
std::set
fifth; // 比較類
bool(*fn_pt)(int,int) = fncomp;
std::set
sixth (fn_pt); // 比較函式指標
return
0;}
1、set::size
:返回容器中的元素數量#include
#include
int main ()
output:
0. size: 0
1. size: 10
2. size: 11
3. size: 10
2、set::empty
:判斷容器中元素數量是否為空(空則返回1)
3、set::begin
:返回指向容器第乙個元素的指標(如果元素為空,則返回不能被引用的空指標),遵循特定的排序順序
4、set::end
:原則上返回容器假設的「最後乙個值的指標」,但是其返回的指標並不能被引用,一般與begin搭配使用用來表示特定的範圍
#include
#include
int main ()
; std::set
myset (myints,myints+5);
std::cout
<< "myset contains:";
for (std::set
::iterator it=myset.begin(); it!=myset.end(); ++it)
std::cout
<< ' '
<< *it;
std::cout
<< '\n';
return
0;}
output:
myset contains: 13
2342
6575
C STL常用模組總結 map
使用之前引用 include定義方法過載函式彙總empty 1 explicit map const key compare comp key compare const allocator type alloc allocator type explicit map const allocator...
C STL 函式模板
模板主要是為了泛型程式設計,做到與型別無關 1.函式模板定義 template 返回型別 函式模板名 呼叫形參表 在返回型別,呼叫形參表和函式體中,所需要型別的地方都可以引用模板形參表中型別形參 templatea function b arg 2.使用 函式模板名 型別形參1,型別形參2,呼叫實參...
C STL之set用法總結
關於set,必須說明的是set關聯式容器。set作為乙個容器也是用來儲存同一資料型別的資料型別,並且能從乙個資料集合中取出資料,在set中每個元素的值都唯一,而且系統能根據元素的值自動進行排序。應該注意的是set中數元素的值不能直接被改變。c stl中標準關聯容器set,multiset,map,m...