C 容器詳解

2021-09-12 12:28:21 字數 4465 閱讀 8768

vector、set、multiset、map和multimap

一:vector

在使用它時, 需要包含標頭檔案#include

vector 容器與陣列相比其優點在於它能夠根據需要隨時自動調整自身的大小以便容下所要放入的元素。此外, vector 也提供了許多的方法來對自身進行操作

二、向量的宣告及初始化

vector 型變數的宣告以及初始化的形式也有許多, 常用的有以下幾種形式

vectora ;                                //宣告乙個int型向量a

vectora(10) ; //宣告乙個初始大小為10的向量

vectora(10, 1) ; //宣告乙個初始大小為10且初始值都為1的向量

vectorb(a) ; //宣告並用向量a初始化向量b

vectorb(a.begin(), a.begin()+3) ; //將a向量中從第0個到第2個(共3個)作為向量b的初始值

int n =  ;

vectora(n, n+5) ; //將陣列n的前5個元素作為向量a的初值

vectora(&n[1], &n[4]) ; //將n[1] - n[4]範圍內的元素作為向量a的初值

三、元素的輸入及訪問

元素的輸入和訪問可以像操作普通的陣列那樣, 用cin>>進行輸入, cout<#includeusing namespace std ;

int main()

;setiset(arr,arr+5);

iset.insert(5);

cout<<"size:"<::iterator ite1 = iset.begin();

set::iterator ite2 = iset.end();

for(;ite1!=ite2;ite1++)

cout如果刪除的話,相同的也一起刪除了;

如果 erase(  )的引數型別是乙個迭代器,那麼即使multiset裡面又多個相同的值的時候也只會刪除乙個

否則就會全部刪除。

舉個例子:

multisets;

s.insert(1);

s.insert(1);

s.erase( s.find(1) ); // 只會刪除乙個1

s.erase( 1 ); // 所有的1 都會刪去

如果查詢的話,返回該元素的迭代器的位置,若有相同,返回第乙個元素的位址;

其他使用和set基本類似。

三:map和multimap

map是stl的乙個關聯容器,它提供一對一(其中第乙個可以稱為關鍵字,每個關鍵字只能在map**現一次,第二個可能稱為該關鍵字的值)的資料;map是根據關鍵字公升序的。

map有三種插入資料的方法

mapmapstudent;

mapstudent.insert(pair(1, "student_one"));

mapstudent.insert(map::value_type (1, "student_one"));

mapstudent[1] = "student_one";

前兩種是沒有區別的,但是最後一種就不一樣了

mapstudent.insert(map::value_type (1, "student_one"));

mapstudent.insert(map::value_type (1, "student_two"));

上面這兩條語句執行後,map中1這個關鍵字對應的值是「student_one」,第二條語句並沒有生效

但是如果是用第三種的話:

mapstudent[1] = "student_one";       mapstudent[1] = "student_two";

map中1的這個關鍵字會被覆蓋。

map 的遍歷

迭代器形式

mapmapstudent;

mapstudent.insert(pair(1, "student_one"));

mapstudent.insert(pair(2, "student_two"));

mapstudent.insert(pair(3, "student_three"));

map::reverse_iterator iter;

for(iter = mapstudent.rbegin(); iter != mapstudent.rend(); iter++)

coutmapmapstudent;

mapstudent.insert(pair(1, "student_one"));

mapstudent.insert(pair(2, "student_two"));

mapstudent.insert(pair(3, "student_three"));

cout《要判定乙個資料(關鍵字)是否在map**現的方法比較多,這裡標題雖然是資料的查詢,在這裡將穿插著大量的map基本用法。

這裡給出三種資料查詢方法

第一種:用count函式來判定關鍵字是否出現,其缺點是無法定位資料出現位置,由於map的特性,一對一的對映關係,就決定了count函式的返回值只有兩個,要麼是0,要麼是1,出現的情況,當然是返回1了

第二種:用find函式來定位資料出現位置,它返回的乙個迭代器,當資料出現時,它返回資料所在位置的迭代器,如果map中沒有要查詢的資料,它返回的迭代器等於end函式返回的迭代器。

查詢map中是否包含某個關鍵字條目用find()方法,傳入的引數是要查詢的key,在這裡需要提到的是begin()和end()兩個成員,

分別代表map物件中第乙個條目和最後乙個條目,這兩個資料的型別是iterator.

#include #include #include using namespace std;    

int main()  

{  mapmapstudent;    

mapstudent.insert(pair(1, "student_one"));   

mapstudent.insert(pair(2, "student_two"));    

mapstudent.insert(pair(3, "student_three"));    

map::iterator iter;    

iter = mapstudent.find(1);    

if(iter != mapstudent.end())    

cout<<"find, the value is "lower_bound函式用法,這個函式用來返回要查詢關鍵字的下界(是乙個迭代器)

upper_bound函式用法,這個函式用來返回要查詢關鍵字的上界(是乙個迭代器)

例如:map中已經插入了1,2,3,4的話,如果lower_bound(2)的話,返回的2,而upper-bound(2)的話,返回的就是3

equal_range函式返回乙個pair,pair裡面第乙個變數是lower_bound返回的迭代器,pair裡面第二個迭代器是upper_bound返回的迭代器,如果這兩個迭代器相等的話,則說明map中不出現這個關鍵字,

程式說明

#include #include #include using namespace std;    

int main()   

{    

mapmapstudent;    

mapstudent[1] = "student_one";    

mapstudent[3] = "student_three";    

mapstudent[5] = "student_five";    

map::iterator iter;    

iter = mapstudent.lower_bound(1);    

//返回的是下界1的迭代器    

couterase(iterator it);//通過乙個條目物件刪除

erase(iterator first,iterator last)//刪除乙個範圍

erase( const key &key );//通過關鍵字刪除

map和multimap的區別 跟set與multiset區別一樣的

multimap 的成員函式 erase() 有 3 個版本:

以待刪除兀素的迭代器作為引數,這個函式沒有返回值;

以乙個鍵作為引數,它會刪除容器中所有含這個鍵的元素,返回容器中被移除元素的個數;

接受兩個迭代器引數,它們指定了容器中的一段元素,這個範圍內的所有元素都會被刪除,這個函式返回的迭代器指向最後乙個被刪除元素的後乙個位置

C 中容器類詳解(三)

8.序列類容器 1 vector向量相當於乙個陣列 在記憶體中分配一塊連續的記憶體空間進行儲存。支援不指定vector大小的儲存。stl內部實現時,首先分配乙個非常大的記憶體空間預備進行儲存,即capacity 函式返回的大小,當超過此分配的空間時再整體重新放分配一塊記憶體儲存,這給人以vector...

C 中的容器類詳解

c 中的容器類包括 順序儲存結構 和 關聯儲存結構 前者包括vector,list,deque等 後者包括set,map,multiset,multimap等。若需要儲存的元素數在編譯器間就可以確定,可以使用陣列來儲存,否則,就需要用到容器類了。1 vector 連續儲存結構,每個元素在記憶體上是連...

C 中的容器類詳解

c 中的容器類包括 順序儲存結構 和 關聯儲存結構 前者包括vector,list,deque等 後者包括set,map,multiset,multimap等。若需要儲存的元素數在編譯器間就可以確定,可以使用陣列來儲存,否則,就需要用到容器類了。1 vector 連續儲存結構,每個元素在記憶體上是連...