国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

C++ STL入門教程(2) list雙向鏈表使用方法(附程序代碼)

2020-01-26 13:57:30
字體:
供稿:網(wǎng)友

一、簡介

“Unlike other standard sequence containers, list and forward_list objects are specifically designed to be efficient inserting and removing elements in any position, even in the middle of the sequence.”

Lists將元素按順序儲存在鏈表中。與向量(vector)相比, 它允許快速的插入和刪除,但是隨機訪問卻比較慢。(vector支持快速隨機訪問)

前一篇就提到過,list可以在頭部進行添加刪除操作,但vector不行。

下面是幾個list特有的函數(shù)。(從另一方面說明list在刪除操作方面的速度之快)

remove() 從list刪除元素
remove_if() 按指定條件刪除元素
reverse() 把list的元素倒轉(zhuǎn)
sort() 給list排序
unique() 刪除list中重復的元素

二、完整程序代碼

/*請務必運行以下程序后對照閱讀*/  #include <list> #include <iostream> #include <algorithm> using namespace std;  void print(int num) {  cout << num << " "; }  bool IsOdd(int i) {  return ((i & 1) == 1); }  int main() {  //1. 初始化  list<int> v;  list<int>::iterator iv;   v.assign(10, 2);//將10個值為2的元素賦到list中  cout << v.size() << endl; //返回list實際含有的元素數(shù)量  cout << endl;   //2. 添加  v.push_front(666);  for (int i = 0; i < 10; i++)   v.push_back(i);  for_each(v.begin(), v.end(), print);//需要#include <algorithm>  cout << endl;  cout << v.size() << endl;  cout << endl;   //3. 插入及遍歷、逆遍歷和倒轉(zhuǎn)  v.insert(v.begin() , 99);//不能+和-了  v.insert(v.end() , 99);   for_each(v.begin(), v.end(), print);  cout << endl;  for_each(v.rbegin(), v.rend(), print);//在逆序迭代器上做++運算將指向容器中的前一個元素  cout << endl;   //一般遍歷寫法  for(iv = v.begin(); iv != v.end(); ++iv)   cout << *iv << " ";  cout << endl;   v.reverse();  for_each(v.begin(), v.end(), print);  cout << endl;  for_each(v.rbegin(), v.rend(), print);  cout << endl;  cout << endl;   //4. 排序  v.sort();//為鏈表排序,默認是升序。  for_each(v.begin(), v.end(), print);  cout << endl;  cout << endl;   //5. 刪除  v.erase(v.begin());  for_each(v.begin(), v.end(), print);  cout << endl;  v.insert(v.begin() , 99);//還原   //刪掉鏈表中所有重復的元素  v.unique();  for_each(v.begin(), v.end(), print);  cout << endl;   //去掉所有含2的元素  v.remove(2);  for_each(v.begin(), v.end(), print);  cout << endl;   //刪掉所有奇數(shù)  v.remove_if(IsOdd);  for_each(v.begin(), v.end(), print);  cout << endl;   v.pop_front();  v.pop_back();  for_each(v.begin(), v.end(), print);  cout << endl;  cout << endl;   //6. 查詢  cout << v.front() << endl;  cout << v.back() << endl;   //7. 清空  v.clear();  cout << v.size() << endl;//0  for_each(v.begin(), v.end(), print); //已經(jīng)clear,v.begin()==v.end(),不會有任何結(jié)果。   return 0; } 

當然,我們也可以用動態(tài)數(shù)組作為保存的數(shù)據(jù)類型:

#include<iostream> #include<string> #include<list> using namespace std;  int main() {  list<char *> li;  list<char *>::iterator iter;  li.push_back("123");  li.push_back("456");  li.push_back("789");  for (iter = li.begin(); iter != li.end(); ++iter)   cout << *iter << endl;  return 0; } 

三、補充

對比vector和list在查詢(隨機檢索)和維護(插入和刪除)上的區(qū)別:

a) 查詢

vector:由于vector中的元素是連續(xù)存儲的,所以我們能夠直接的訪問第n個元素。

list:由于list中的元素不是在內(nèi)存中連續(xù)存儲的,下一個元素的內(nèi)存地址保存在前一個元素中,所以我們必須一個一個的訪問前面的元素,最后才能訪問第n個元素。

當然,對于順序訪問就二者就差不多了。

b) 維護

vector:在vector中插入/刪除一個元素的話,我們需要移動插入/刪除位置之后的所有元素。如果在vector插入/刪除元素后有大量元素的情況下,顯而易見,這些移動和刪除操作會大量的消耗CPU時間。

list:使用list進行這些操作的時候,其僅僅是修改插入/刪除元素之前的元素到后一個元素的指針則可以完成這些操作,這樣可以節(jié)約大量的CPU時間。

參考網(wǎng)站:http://www.cplusplus.com/reference/list/list/

以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持武林網(wǎng)。

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 仙居县| 郎溪县| 云安县| 垣曲县| 大关县| 忻城县| 福安市| 普格县| 旅游| 莱西市| 安康市| 深水埗区| 锦州市| 寻乌县| 神农架林区| 沙湾县| 金秀| 昌图县| 盐边县| 科技| 佳木斯市| 商水县| 封丘县| 寿光市| 黄浦区| 宁河县| 澎湖县| 丹棱县| 松阳县| 花莲县| 抚州市| 武安市| 万源市| 延长县| 政和县| 成武县| 安义县| 黑河市| 鄂托克前旗| 吴川市| 桐城市|