C++ primer 順序容器

來源:互聯網
上載者:User

順序容器:vector     deque   list

適配器:根據原始容器類型所提供的操作,通過定義新的操作介面,適應基礎的容器類型。

 

容器元素初始化:

 C<T> c;     //預設建構函式

 C<T>c(n);   //n個值初始化,只適用於順序容器,必須給出預設建構函式

 C<T>c(n,t);    //n個t  只適用於順序容器,可以不給,但要給出 T(t)建構函式。

 C<T>c(begin,end);  //begin,end複製到c中

 C<T> c1(c2);//c1和c2是相同的容器類型,存放相同類型元素,c2拷貝給c1

 

如果想要把不同容器的元素相互賦值,則可用第4個建構函式或用assign。

迭代器的行為像指標。

 

用作容器類型要遵循2個條件

1.能賦值運算。

2.能複製。

注意:參考型別不能複製和賦值。IO流也不支援複製。

 

vector deque隨機儲存

list 鏈表

 

注意迭代器失效問題。

 

difference_type 儲存兩個迭代器差值的有符號整型,可為負數

value_type 元素類型

reference 元素左實值型別,value_type&

const_reference 元素常量左實值型別,const value_type&

size_type 無符號整型

const_iterator

reverse_iterator

const_reverse_iterator

 

時刻更新迭代器,不然會出現迭代器失效問題。

insert都是插在iter的前面。

iterator insert(iter,n); //n插入iter前面,返回新元素的迭代器

void insert(iter,begin,end); //begin end插入iter前面。

void insert(iter,n,t); n個t插入iter前。

 

如果容器元素支援的比較符,可以讓容器整體比較。

如 vector<int> c1,c2;     c1==c2;

 

c.size()

c.max_size();

c.empty();

c.resize(n);  //改變c的長度

c.resize(n,t);

 

reference c.back() ;  //得到最後元素的引用

reference c.front();   //得到最前面元素的引用

c.at(n)==c[n];

 

c.clear();//清空

c.erase(iter); //返回iter後面的元素

c.erase(begin,end);//返回begin,end後面的元素

 

pop_front()和front()經常一起使用。

iterator  find(begin,end,value);//找到value出現的第一個地方。

 

string 增加的建構函式:cp為空白字元結尾的數組

1.string s(cp,n); //cp指向的數組的前n個元素。

2.string s(s2,pos2);//s2中從pos2開始複製給s

3.string s(s2,pos2,len);//s2中從pos2開始複製len個字元給s

 

string特有函數:

1.s.insert(pos,s2);//pos前插入string對象,,,pos為int

2.s.insert(pos,s2,pos,len);//pos前插入s2從pos開始的len長度

3.s.insert(pos,cp,len);//pos前插入cp的前len個字元

4.s.insert(pos,cp);//pos前插入cp

5.s.assign(s2); //s2賦值給s

6.s.assign(cp,len);//cp的len個字元給s

7.s.assign(cp);//cp賦給s

8.s.erase(pos,len);//刪除從pos開始的len長

 

 

9.s.substr(pos,n);//返回一個string字串,包含從pos開始的n個字元。

10.s.substr(pos);//返回string字串,包含從pos開始的

11.s.substr();//返回s的副本

 

string中的append 和replace函數,返回s的引用

1.s.append(arg);//將arg插在s的後面。

2.s.replace(pos,len,args);//將args的元素替換 pos開始長為len的字元。

3.s.replace(b,e,args);//args的元素替換b,e 的所有字元

 

args可為:

1.s2

2.s2,pos,len

3.cp

4.cp,len

5.n,c

6.b2,e2; //迭代器

 

string提供的尋找函數

1.s.find(args);

2.s.rfind(args);

3.s.find_first_of(args);//args中任一字元第一次出現的位置

4.s.find_last_of(args);

5.s.find_first_not_of(args);//尋找第一個不屬於args的字元

6.s.find_last_not_of(args);//最後一個不屬於args的字元

 

args為:

1.c,pos   //從pos開始,尋找字元c,預設pos=0

2.s2,pos //pos開始尋找string s2 pos預設為0

3.cp,pos  //pos開始尋找cp(cstring)   pos預設為0

4.cp,pos,n //pos開始尋找cp所指向的數組的前n個字元

 

string 的compare函數

1.s.compare(s2);

2.s.compare(pos1,n1,s2); //從pos1開始的n1個字元與s2做比較

3.s.compare(pos1,n1,s2,pos2,n2); //從pos1開始的n1個字元與s2從pos2開始的n2個字元比較

4.s.compare(cp) ;//與char* 比較

5.s.compare(pos1,n1,cp);//從pos1 開始的n1個字元與cp比較

6.s.compare(pos1,n1,cp,n2);//pos1開始的n1個字元與cp的前n2個字元比較。

 

 

適配器:容器適配器,迭代器適配器,函數適配器

預設

deque stack和queue 

vector priority_queue

當然可以改變關聯容器。

如stack<string,vector<string>>

stack的關聯容器為順序容器

queue的關聯容器必須支援push_front();

priority_queue 的關聯容器必須提供隨機訪問。

priority_queue設定優先權插入。

 

 

 

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.