一 各種容器測試

2021-10-12 04:37:38 字數 4201 閱讀 4623

查詢速度慢

void

test_vector()

catch

(exception &p)

} cout <<

"size = "

<< v.

size()

<< endl;

cout <<

"capcity = "

<< v.

capacity()

<< endl;

cout <<

"front = "

<< v.

front()

<< endl;

cout <<

"back = "

<< v.

back()

<< endl;

string target =

"123"

;// 查詢;find返回是迭代器型別

auto pitem =

::find

(v.begin()

, v.

end(

), target);if

(pitem != v.

end())

else

// 排序

::sort

(v.begin()

, v.

end())

;}

void

test_list()

catch

(exception &p)

} cout <<

"size = "

<< l.

size()

<< endl;

cout <<

"maxsize = "

<< l.

max_size()

<< endl;

cout <<

"front = "

<< l.

front()

<< endl;

cout <<

"back = "

<< l.

back()

<< endl;

// 查詢,list

string target =

"123"

;auto pitem =

::find

(l.begin()

, l.

end(

), target);if

(pitem != l.

end())

else

// 排序,list自帶演算法sort

l.sort()

;}

void

test_list()

catch

(exception &p)

} cout <<

"size = "

<< q.

size()

<< endl;

cout <<

"maxsize = "

<< q.

max_size()

<< endl;

cout <<

"front = "

<< q.

front()

<< endl;

cout <<

"back = "

<< q.

back()

<< endl;

// 查詢,deque

string target =

"123"

;auto pitem =

::find

(q.begin()

, q.

end(

), target);if

(pitem != q.

end())

else

// 排序,list自帶演算法sort

::sort

(q.begin()

, q.

end())

;}

插入元素慢

查詢元素快

元素可重複

void

test_muiliset()

catch

(exception &p)

} cout <<

"size = "

<< c.

size()

<< endl;

cout <<

"max size = "

<< c.

max_size()

<< endl;

// 全域性find

string target =

"123"

;auto pitem1=

::find

(c.begin()

, c.

end(

), target);if

(pitem1 != c.

end())

else

// 專屬find

auto pitem2 = c.

find

(target);if

(pitem1 != c.

end())

else

}

key可重複

void

test_multimap()

catch}

cout <<

"size = "

<< c.

size()

<< endl;

cout <<

"maxsize = "

<< c.

max_size()

<< endl;

long target =

100;

// 查詢pair

auto pitem = c.

find

(target);if

(pitem != c.

end())

else

}

元素不可重複

自動去重

void

test_set()

catch

(exception &p)

} cout <<

"size = "

<< c.

size()

<< endl;

cout <<

"max size = "

<< c.

max_size()

<< endl;

// 全域性find

string target =

"123"

;auto pitem1=

::find

(c.begin()

, c.

end(

), target);if

(pitem1 != c.

end())

else

// 專屬find

auto pitem2 = c.

find

(target);if

(pitem1 != c.

end())

else

}

key不可重複,value可重複

自動去重(key)

void

test_map()

catch}

cout <<

"size = "

<< c.

size()

<< endl;

cout <<

"maxsize = "

<< c.

max_size()

<< endl;

long target =

100;

// 查詢pair

auto pitem = c.

find

(target);if

(pitem != c.

end())

else

}

第二講 容器分類與各種測試

一 容器 分為序列容器 關聯容器 不定續容器 不定續容器 在資料存入容器中,位置不確定 序列容器 都有一定的次序,按照放進去的次序 array 用key來找value就非常快,所以比較適合查詢。vector deque 雙向 list 雙向鍊錶 forward list 單向鍊錶 關聯容器 set ...

容器之分類與各種測試(四) multimap

multiset和multimap的具體區別在於,前者的key值就是自己儲存的value,後者的key與value是分開的不相關的。例程 include include include include include include include includeusing namespace st...

容器之分類與各種測試(三) stack

stack是棧,其實現也是使用了雙端佇列 只要不用雙端佇列的一端,僅用單端資料進出即完成單端佇列的功能 由於queue和stack的實現均是使用deque,沒有自己的資料結構和演算法,所以這倆也被稱為容器介面卡 container adapter 例程 include include include...