9久久伊人精品综合,亚洲一区精品视频在线,成 人免费va视频,国产一区二区三区黄网,99国产精品永久免费视频,亚洲毛片多多影院,精品久久久无码人妻中文字幕,无码国产欧美一区二区三区不卡
學習啦>學習英語>專業英語>計算機英語>

c中list的用法

時間: 長思709 分享

  下面小編就跟你們詳細介紹下c中list的用法的用法,希望對你們有用。

  c中list的用法的用法如下:

  這幾天在做圖像處理方面的研究,其中有一部分是關于圖像分割方面的,圖像目標在分割出來之后要做進一步的處理,因此有必要將目標圖像的信息保存在一個變量里面,一開始想到的是數組,但是馬上就發現使用數組的缺點:數組長度固定,動態分配內存很容易導致錯誤發生。最重要的一點是我要保存目標圖像的每一點的坐標值,使用數組就有點無能為力了。因此到百度、Google大神上面找思路,終于被我發現在c++的標準庫里面還有這么一個模板類:list,下面就是對找到的資料的匯總和加工。

  vc6自帶的msdn幫助文檔的解釋

  以下是引自msdn幫助文檔:

  The template class describes an object that controls a varying-length sequence of elements of type T. The sequence is stored as a bidirectional linked list of elements, each containing a member of type T.

  本模板類描述了一個對象,這個對象是類型為T的可變長度的序列元素。這個序列采用雙向鏈表的方式存儲每一個元素,其中每一個元素的數據流行都是T。

  The object allocates and frees storage for the sequence it controls through a protected object named allocator, of class A. Such an allocator object must have the same external interface as an object of template class allocator. Note that allocatoris not copied when the object is assigned.

  對序列對象的分配和釋放操作通過一個受保護的對象allocator進行。這樣一個allocator對象必須有相同的外部接口作為一個模板類分配器的對象。注意:當對象被分配之后allocator不能被復制。

  List reallocation occurs when a member function must insert or erase elements of the controlled sequence. In all such cases, only iterators or references that point at erased portions of the controlled sequence become invalid.

  當一個成員要進行insert或者erase操作時,列表的重新分配操作發生。在這種情況下,只有迭代器或者引用所指向的要刪除的對象的指針變為無效。

  msdn幫助文檔自帶的例子

  下面為msdn幫助文檔中自帶的一個例子,該例展示了如何使用迭代器讀取列表中的元素和進行插入操作。

  #include <list>

  #include <iostream>

  using namespace std ;

  typedef list<int> LISTINT;

  void main()

  {

  int rgTest1[] = {5,6,7};

  int rgTest2[] = {10,11,12};

  LISTINT listInt;

  LISTINT listAnother;

  LISTINT::iterator i;

  // Insert one at a time

  listInt.insert (listInt.begin(), 2);

  listInt.insert (listInt.begin(), 1);

  listInt.insert (listInt.end(), 3);

  // 1 2 3

  for (i = listInt.begin(); i != listInt.end(); ++i)

  cout << *i << " ";

  cout << endl;

  // Insert 3 fours

  listInt.insert (listInt.end(), 3, 4);

  // 1 2 3 4 4 4

  for (i = listInt.begin(); i != listInt.end(); ++i)

  cout << *i << " ";

  cout << endl;

  // Insert an array in there

  listInt.insert (listInt.end(), rgTest1, rgTest1 + 3);

  // 1 2 3 4 4 4 5 6 7

  for (i = listInt.begin(); i != listInt.end(); ++i)

  cout << *i << " ";

  cout << endl;

  // Insert another LISTINT

  listAnother.insert (listAnother.begin(), rgTest2, rgTest2+3);

  listInt.insert (listInt.end(), listAnother.begin(), listAnother.end());

  // 1 2 3 4 4 4 5 6 7 10 11 12

  for (i = listInt.begin(); i != listInt.end(); ++i)

  cout << *i << " ";

  cout << endl;

  }

  Program Output is:

  1 2 3

  1 2 3 4 4 4

  1 2 3 4 4 4 5 6 7

  1 2 3 4 4 4 5 6 7 10 11 12

  list::list模板類的主要函數介紹

  assign() //給list賦值

  back() //返回最后一個元素

  begin() //返回指向第一個元素的迭代器

  clear() //刪除所有元素

  empty() //如果list是空的則返回true

  end() //返回末尾的迭代器

  erase() //刪除一個元素

  front() //返回第一個元素

  get_allocator() //返回list的配置器

  insert() //插入一個元素到list中

  max_size() //返回list能容納的最大元素數量

  merge() //合并兩個list

  pop_back() //刪除最后一個元素

  pop_front() //刪除第一個元素

  push_back() //在list的末尾添加一個元素

  push_front() //在list的頭部添加一個元素

  rbegin() //返回指向第一個元素的逆向迭代器

  remove_if() //按指定條件刪除元素

  remove() //從list刪除元素

  rend() //指向list末尾的逆向迭代器

  resize() //改變list的大小

  reverse() //把list的元素倒轉

  size() //返回list中的元素個數

  sort() //給list排序

  splice() //合并兩個list

  swap() //交換兩個list

  unique() //刪除list中重復的元素

  常用的操作主要是有插入操作、刪除操作。list為實現頭尾高效的插入和刪除操作而提供了大多數的支持函數,而對于隨機訪問函數,則只能從頭部或者尾部進行遍歷操作。

  關于remove和erase函數

  上面的介紹中關于插入等等操作都有幫助的例子,但是對于刪除函數,這個需要有一些注意的地方。下面請看例子:

  #include <iostream>

  #include <list>

  #include <numeric>

  #include <algorithm>

  using namespace std;

  //創建一個list容器的實例LISTINT

  typedef list<int> TESTINT;

  void main()

  {

  //使用TESTINT創建一個list類型的對象

  TESTINT test;

  //使用TESTINT創建一個迭代器對象

  TESTINT:iterator i;

  //從前面向listOne容器中添加數據

  test.push_front (2);

  test.push_front (1);

  //從后面向listOne容器中添加數據

  test.push_back (3);

  test.push_back (4);

  //從列表中刪除一個元素

  i = test.begin();

  while(i != test.end())

  {

  int tmp = *i;

  if(tmp == 2){

  test.erase(i++);//在這里要是指針前進1個,否則迭代器失效

  }else{

  i++;

  }

  }

  }

537205 主站蜘蛛池模板: 国产亚洲久久久久久久| 视频二区中文字幕在线| 亚洲国产成人av国产自| 国产亚洲AV电影院之毛片| 欧美日本精品一本二本三区| 午夜福利在线观看入口| 成人拍拍拍无遮挡免费视频| 成人国产精品日本在线观看| 免费人成网上在线观看网址| 免费AV片在线观看网址| 亚洲国产成人精品综合色| 乱60一70归性欧老妇| 国产乱人伦偷精品视频下| 亚洲不卡一区三区三区四| 884aa四虎影成人精品| 人人妻人人狠人人爽| 亚洲AV永久无码天堂网一线| 色成年激情久久综合国产| 国产内射性高湖| 日本一区二区三区在线播放| 色噜噜一区二区三区| 日韩免费视频一一二区| 精品精品亚洲高清a毛片| 欧美性猛交xxxx免费看| 老色鬼在线精品视频在线观看 | 国产激情av一区二区三区| 国产360激情盗摄全集| 五月综合网亚洲乱妇久久| 精品999日本久久久影院| 国产成人午夜福利精品| 国产老熟女国语免费视频| 99久热在线精品视频| 亚洲国产精品一区二区视频| 2019国产精品青青草原| 国产亚洲中文字幕久久网| 精品少妇人妻av无码专区| 国产亚洲女人久久久精品| 极品vpswindows少妇| 国产一区二区三区日韩精品| 成人午夜激情在线观看| 91精品乱码一区二区三区|