C STLmap的使用和特點 C STL相關

2021-10-02 02:46:00 字數 3783 閱讀 3288

map是c++98中引入的二叉樹資料結構

1、map有四個引數

2、count()和find()函式傳入的引數都是key

3、*finditer迭代器返回的是const int

4、在map中插入元素需要用make_pair

5、insert有返回兩個引數第乙個是迭代器的位置,第二個表示插入是否成功

6、emplace比insert插入效率更高

7、以傳入的型別需要有預設的建構函式

8、用at定位會丟擲異常,一般配合try catch使用

9、如果用以下方法賦值會重新生成pair進行拷貝建構函式賦值由於不是同乙個東西,影響效率

std::pair<

int,std::string> obj;

obj =

*finditer;

應該用引用的方式:

const std::pair<

int, std::string>

& obj =

*finditer;

10、查詢元素一般用find比較多

auto finditer = b.

find(10

);if(finditer != std::

end(b)

/* b.end() */

)else

也可以自己定義模板獲取值,更高效一點

template

<

class

map>

(const map &map,

const

typename map::key_type &key,

const

typename map::()

)

static

void()

// 特點自動將元素排序

// 插入和刪除查詢o(logn)

// 必須key元素必須支援嚴格的弱順序

// (1) x < y == true, y < x == false

// (2) x < y == true, y < z == true, x < z == true

// (3) x < x === false

// (4) a == b, b == c, c == a

// 不能改變key元素的值

//using group = std::map<

int,std::string>

; group a;

group b = a;

group c

(a);

group d

(c.begin()

, c.

end())

; group g(,

,});

d.empty()

; d.

size()

; d.

max_size()

;// operator == != < > <= >=

// special

auto keycomp = c.

key_comp()

;auto valuecomp = c.

value_comp()

;// 賦值

b = g;

// 交換

b.swap

(a);

std::

swap

(a,b)

;// 迭代器相關

a.begin()

; a.

end();

a.cbegin()

; a.

cend()

; a.

rbegin()

; a.

rend()

; a.

crbegin()

; a.

crend()

;auto iterbegin = a.

begin()

;//演算法函式

// map count 0 1

// multimap count >= 0

auto num = a.

count(1

);auto finditer = a.

find(1

);if(finditer == a.

end())

else

auto lower = a.

lower_bound(1

);if(lower != a.

end())

}auto high = a.

upper_bound(1

);auto range = a.

equal_range(1

);// return std::make_pair(a.lower_bound(1), a.upper_bound(1));

auto eraseiter = b.

erase

(b.begin()

);eraseiter = b.

erase

(b.begin()

, b.

end())

;auto state = b.

insert

(std::

make_pair

(100

,"good"))

;// state is a pair

auto insertiter = b.

insert

(c.begin()

, c.

end())

; b.

emplace

(std::

make_pair(10

,"has it"))

; b.

emplace(11

, std::

string

("again"))

;// b.emplace( std::pair(10, std::string("again"));

b.emplace(12

,"third");

// b.emplace(std::pair(10, "third"));

b.emplace_hint

(b.end()

,13,"haha");

// // auto string

auto

& info = b[10]

;// -> auto iter = b.insert(std::make_pair(13, std::string())).first; return (*iter).second;

b[13];

// at

trycatch(.

..)auto finditer = b.

find(10

);if(finditer != std::

end(b)

/* b.

end()*

/)else

auto info =

get_default

(b,10);

if(info.

empty()

)else

}template

<

class

map>

(const map &map,

const

typename map::key_type &key,

const

typename map::()

)

C S 和B S的區別及特點

cs即client server 客戶機 伺服器 結構。優點 c s結構在技術上很成熟,它的主要特點是 互動性強 具有安全的訪問模式 網路通訊量低 響應速度快 利於處理大量資料 缺點 但是該結構的程式是 針對性開發,變更不夠靈活,維護和管理的難度較大。通常只侷限於小型區域網,不利於擴充套件。並且,由...

C S結構與B S結構的特點分析

為了區別於傳統的c s模式,才特意將其稱為b s模式。認識到這些結構的特徵,對於系統的選型而言是很關鍵的。1 系統的效能 在系統的效能方面,b s占有優勢的是其異地瀏覽和資訊採集的靈活性。任何時間 任何地點 任何系統,只要可以使用瀏覽器上網,就可以使用b s系統的終端。不過,採用b s結構,客戶端只...

CS結構與BS結構的特點分析

為了區別於傳統的c s模式,才特意將其稱為b s模式。認識到這些結構的特徵,對於系統的選型而言是很關鍵的。1 系統的效能 在系統的效能方面,b s占有優勢的是其異地瀏覽和資訊採集的靈活性。任何時間 任何地點 任何系統,只要可以使用瀏覽器上網,就可以使用b s系統的終端。不過,採用b s結構,客戶端只...