Some C++/ STL Container

Copyright @ Joel Jiang(江東敏) at 2017.07.21 13:00 p.m

in Shenzhen.China. LIST- TE- E11 -01


1.Deque

Deque(usually pronounced like"deck") is an irregular acronym ofdouble-endedqueue. Double-ended queues are sequence containers with dynamic sizes that can be expanded or contracted on both ends (either its front or its back).


"In computer science, a queue is a particular kind of abstract data type or collection in which the entities in the collection are kept in order and the principal (or only) operations on the collection are the addition of entities to the rear terminal position, known as en queue,and removal of entities from the front terminal position, known as dequeue. This makes the queue a First-In-First-Out (FIFO) data structure. In a FIFO data structure, the first element added to the queue will be the first one to be removed. This is equivalent to the requirement that once a new element is added, all elements that were added before have to be removed before the new element can be removed. Often a peek or front operation is also entered, returning the value of the front element without dequeuing it. A queue is an example of a linear data structure, or more abstractly a sequential collection."


Queues provide services in computer science,transport, and operations research where various entities such as data, objects, persons, or events are stored and held to be processed later. In these contexts, the queue performs the function of a buffer.

Deque 和vector內存管理不同: 大塊分配內存!屬于雙向隊列, 和vector類似, 新增加:

1.push_front 在頭部插入一個元素

2.pop_front 在頭部彈出一個元素

對于deque和vector來說,盡量少用erase(pos)和erase(beg,end)。因為這在中間刪除數據后會導致后面的數據向前移動,從而使效率低下。

Some API :

new Deque()

new Deque(Array items)

new Deque(int capacity)

push(dynamic items...)

unshift(dynamic items...)

pop()

shift()

toArray()

peekBack()

peekFront()

get(int index)

isEmpty()

clear()


2》Stack

實際底層也是使用Deque實現, 但也可以用實際制定容器Container

先進后出結構? 只有一個出口 ,且只能訪問頂端元素, 不允許遍歷 支持操作:?
1.push增加元素
2.pop移除元素
3.top獲取頂端元素


3.Queue

queues are a type of container adaptor, specifically designed to operate in a FIFO context (first-in first-out), where elements are inserted into one end of the container and extracted from the other.

queues are implemented as containers adaptors, which are classes that use an encapsulated object of a specific container class as its? underlying container, providing a specific set of member functions to access its elements. Elements are pushed into the"back"of the specific container andpopped from its"front".

實際底層使用Deque實現, 但也可以實際制定容器Container

先進先出結構, 兩個出口 ,不允許遍歷 ,支持操作:
1.push增加元素
2.pop移除元素
3.front獲取最前端元素
4.back獲取最后的元素


4. Map

關聯容器, 存儲對象為key/value pair ,但不允許重復 key ,map存儲對象必須具備可排序性

類的聲明: 
template < class _Kty, class _Ty, class _Pr = lass<_Kty>, class _Alloc = allocator < pair < const _Kty, _Ty> > >?
class map{…………};

1, 兩個> > 之間是有一個空格
2, 其中默認使用lass定義排序行為, 所以我們可以自定義排序行為 - 仿函數實現
3, 注意各種class的順序!!! 注意排序所使用的可以是在哪里!!!

a.定義一個類
struct Employee{
? ? Employee(string& s1): Name(s1){}
? ? string Name;
};

b.定義一個仿函數
struct ReverseId: public std::binary_function< int, int, bool>{
? ? bool operator() (const int& key1, const int& key2) const {
? ? return (key1 <= key2) ? false : true;
? ? }
};
仿函數就是要重載()小括號操作符! ! !

使用例子:
c. 構建一個序列:
std::pair < int, Employee> item[3] = {
? ? std::make_pair(1, Employee(“Tom”)),
std::make_pair(2, Employee(“Azm”)),
std::make_pair(3, Employee(“Jack”)),
};

d.定義一個map,是一個按照我們制定排序方法的map
std::map < int, Employee, ReverseId > map1(item, item+3);

e. 在map中插入元素
方法1 : map1.insert(std::make_pair(4,Employee(“Jason”)));
方法2 : map1[5] = Employee(“Hellon”);

f. 刪除元素
std::map < int, Employee>::iterator it = map1.begin();
map1.erase(it);

g. 使用[]操作符存取元素
Employee& e = map1[14];
e.SetName(“Wason”);


5. Multimap

Multimaps are associative containers that store elements formed by a combination of akey valueand amapped value, following a specific order, and where multiple elements can have equivalent keys.

它類似map的關聯容器 ,允許key重復!

查找

1. 直接找到每種鍵值的所有元素的第一個元素的游標

通過函數:lower_bound( const keytype& x ), upper_bound( const keytype& x ) 可以找到比指定鍵值x的小的鍵值的第一個元素和比指定鍵值x大的鍵值的第一個元素。返回值為該元素的游標。

細節:當到達鍵值x已經是最大時,upper_bound返回的是這個multimap的end游標。同理,當鍵值x已經是最小了,lower_bound返回的是這個multimap的begin游標。

2. 指定某個鍵值,進行遍歷

可以使用上面的lower_bound和upper_bound函數進行游歷,也可以使用函數equal_range。其返回的是一個游標對。游標對pair::first是由函數lower_bound得到的x的前一個值,游標對pair::second的值是由函數upper_bound得到的x的后一個值。

multimap<int ,int > a;

a.insert(pair(1,11));

a.insert(pair(1,12));

a.insert(pair(1,13));

a.insert(pair(2,21));

a.insert(pair(2,22));

a.insert(pair(3,31));

a.insert(pair(3,32));

multimap::iterator p_map;

pair::iterator, multimap::iterator> ret;

for(p_map = a.begin() ; p_map != a.end();) {?

cout";

ret = a.equal_range(p_map->first);

for(p_map = ret.first; p_map != ret.second; ++p_map)

cout<<""<< (*p_map).second;

cout<<endl;

}

std::multimap < int, Employee, ReverseId > mm1(item, item+3);
#如果插入一個重復的key=1的key:
map1.insert(std::make_pair(4,Employee(“Peter”)));
#則:?
mm1.count(4)? //得到2? 表名其中有兩個key為4的元素



6.Set

Sets are containers that store unique elements following a specific order.

In aset, the value of an element also identifies it (the value is itself thekey, of typeT), and each value must be unique. The value of the elements in asetcannot be modified once in the container (the elements are always const), but they can be inserted or removed from the container.

set初始化:

struct Programmer{

Programmer(const int id, const std::wstring name):

Id(id), Name(name){? }

void Print() const

{

std::wcout<

}

int Id;

std::wstring Name;

};

set相關算法:?
1. set_union
std::set < Programmer, ProgrammerIdGreater > dest;
std::insert_iterator < std::set < Programmer, ProgrammerIdGreater > > ii(dest, dest.begin());

std::set_union(ps1.begin(), ps1.end(), ps2.begin(), ps2.end(), ii, ProgrammerIdGreater());
將會把ps1和ps2合并到dest當中,這里將會依照給出的排序規則進行排序!

2, set_intersection
std::set < Programmer, ProgrammerIdGreater > dest;
std::insert_iterator < std::set < Programmer, ProgrammerIdGreater > > ii(dest, dest.begin());

std::set_intersection(ps1.begin(), ps1.end(), ps3.begin(), ps3.end(), ii, ProgrammerIdGreater());
將會把ps1和ps3中全部重復的元素提取出來放在dest中! 這里將會依照給出的排序規則進行排序!


The sharing of knowledge, the spirit of encouragement.

By Joel Jiang (江東敏)


最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
  • 序言:七十年代末,一起剝皮案震驚了整個濱河市,隨后出現的幾起案子,更是在濱河造成了極大的恐慌,老刑警劉巖,帶你破解...
    沈念sama閱讀 227,702評論 6 531
  • 序言:濱河連續發生了三起死亡事件,死亡現場離奇詭異,居然都是意外死亡,警方通過查閱死者的電腦和手機,發現死者居然都...
    沈念sama閱讀 98,143評論 3 415
  • 文/潘曉璐 我一進店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人,你說我怎么就攤上這事。” “怎么了?”我有些...
    開封第一講書人閱讀 175,553評論 0 373
  • 文/不壞的土叔 我叫張陵,是天一觀的道長。 經常有香客問我,道長,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 62,620評論 1 307
  • 正文 為了忘掉前任,我火速辦了婚禮,結果婚禮上,老公的妹妹穿的比我還像新娘。我一直安慰自己,他們只是感情好,可當我...
    茶點故事閱讀 71,416評論 6 405
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著,像睡著了一般。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發上,一...
    開封第一講書人閱讀 54,940評論 1 321
  • 那天,我揣著相機與錄音,去河邊找鬼。 笑死,一個胖子當著我的面吹牛,可吹牛的內容都是我干的。 我是一名探鬼主播,決...
    沈念sama閱讀 43,024評論 3 440
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了?” 一聲冷哼從身側響起,我...
    開封第一講書人閱讀 42,170評論 0 287
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎,沒想到半個月后,有當地人在樹林里發現了一具尸體,經...
    沈念sama閱讀 48,709評論 1 333
  • 正文 獨居荒郊野嶺守林人離奇死亡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內容為張勛視角 年9月15日...
    茶點故事閱讀 40,597評論 3 354
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發現自己被綠了。 大學時的朋友給我發了我未婚夫和他白月光在一起吃飯的照片。...
    茶點故事閱讀 42,784評論 1 369
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖,靈堂內的尸體忽然破棺而出,到底是詐尸還是另有隱情,我是刑警寧澤,帶...
    沈念sama閱讀 38,291評論 5 357
  • 正文 年R本政府宣布,位于F島的核電站,受9級特大地震影響,放射性物質發生泄漏。R本人自食惡果不足惜,卻給世界環境...
    茶點故事閱讀 44,029評論 3 347
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧,春花似錦、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 34,407評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至,卻和暖如春,著一層夾襖步出監牢的瞬間,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 35,663評論 1 280
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留,地道東北人。 一個月前我還...
    沈念sama閱讀 51,403評論 3 390
  • 正文 我出身青樓,卻偏偏與公主長得像,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子,可洞房花燭夜當晚...
    茶點故事閱讀 47,746評論 2 370

推薦閱讀更多精彩內容