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

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

C++List模板類的使用

2019-11-06 07:49:44
字體:
來源:轉載
供稿:網友

List的使用

在使用list必須包括頭文件#include <list>

如何定義一個list對象

[cpp] view plain copy #include <list>  int main (void)  {      list<char > cList; //聲明了list<char>模板類 的一個實例  }  

使用list的成員函數push_back和push_front插入一個元素到list中

cList. push_back(‘a’); //把一個對象放到一個list的后面

cList. push_front (‘b’); //把一個對象放到一個list的前面

使用list的成員函數empty()判斷list是否為空

[cpp] view plain copyif (cList.empty())  {      PRintf(“this list is empty”);  }  

用list< char >::iterator得到指向list的指針

[cpp] view plain copy list< char>::iterator charIterator;  for(cIterator = cList.Begin();cIterator != cList.end();cIterator++)  {      printf(“%c”, *cIterator);  } //輸出list中的所有對象  

說明:cList.Begin()和cList.end()函數返回指向list< char >::iterator的指針,由于list采用鏈表結構,因此它不支持隨機存取,因此不能用cList.begin()+3來指向list中的第 四個對象,vector和deque支持隨機存取。

用STL的通用算法count()來統計list中的元素個數

int cNum;

char ch = ’b’;

cNum = count(cList.Begin(), cList.end(), ch); //統計list中的字符b的個數

說明:在使用count()函數之前必須加入#include <algorithm>

用STL的通用算法count_if ()來統計list中的元素個數

[cpp] view plain copy const char c(‘c’);  class IsC  {  public:      bool Operator() ( char& ch )      {          return ch== c;      }  };    int numC;  numC = count_if (cList.begin(), cList.end(),IsC());//統計c的數量;  

說明:count_if() 帶一個函數對象的參數,函數對象是一個至少帶有一個operator()方法的類函數對象被約定為STL算法調用operator時返回true或 false。它們根據這個來判定這個函數。舉個例子會 說的更清楚些。count_if()通過傳遞一個函數對象來作出比count()更加復雜的評估以確定一個對象是否應該被記數。

使用STL通用算法find()在list中查找對象

[cpp] view plain copy list<char >::iterator FindIterator;  FindIterator = find(cList.begin(), cList.end(), ‘c’);  If (FindIterator == cList.end())  {      printf(“not find the char ‘c’!”);  }  else  {      printf(“%c”, * FindIterator);  }  

說明:如果沒有找到指定的對象,就會返回cList.end()的值,找到了就返回一個指向對象iterator的指針。

使用STL通用算法find_if()在list中查找對象

[cpp] view plain copy const char c(‘c’);  class c  {  public:      bool operator() ( char& ch )      {          return ch== c;      }  };    list<char>::iterator FindIterator  FindIterator = find_if (cList.begin(), cList.end(),IsC());//查找字符串c;  

說明:如果沒有找到指定的對象,就會返回cList.end()的值,找到了就返回一個指向對象iterator的指針。

使用list的成員函數sort()排序

cList.sort();

使用list的成員函數insert插入一個對象到list中

[cpp] view plain copy cList.insert(cLiset.end, ‘c’); ///在list末尾插入字符‘c’    char ch[3] ={‘a’, ‘b’, ‘c’};  cList.insert(cList.end, &ch[0], & ch[3] ); //插入三個字符到list中  說明:insert()函數把一個或多個元素插入到指出的iterator位置。元素將出現在 iterator指出的位置以前。  如何在list中刪除元素  cList.pop_front(); //刪除第一個元素  cList.pop_back(); //刪除最后一個元素  cList. Erase(cList.begin()); //使用iterator刪除第一個元素;  cList. Erase(cList.begin(), cList.End()); //使用iterator刪除所有元素;  cList.remove(‘c’); //使用remove函數刪除指定的對象;    list<char>::iterator newEnd;  //刪除所有的’c’ ,并返回指向新的list的結尾的iterator  newEnd = cList.remove(cList.begin(), cList.end(), ‘c’);  
發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表

圖片精選

主站蜘蛛池模板: 民县| 津南区| 凯里市| 兴安盟| 汾西县| 西乌| 大方县| 古丈县| 柏乡县| 阜新市| 密山市| 龙岩市| 孝昌县| 巨野县| 特克斯县| 夹江县| 新丰县| 准格尔旗| 扎囊县| 德安县| 孟村| 建平县| 鹿邑县| 庆城县| 屯留县| 延津县| 富源县| 长葛市| 邓州市| 乌拉特后旗| 赫章县| 宜丰县| 天长市| 通化县| 拉孜县| 都匀市| 勐海县| 博客| 房产| 东台市| 云林县|