六月丁香五月婷婷,丁香五月婷婷网,欧美激情网站,日本护士xxxx,禁止18岁天天操夜夜操,18岁禁止1000免费,国产福利无码一区色费

學習啦>學習英語>專業(yè)英語>計算機英語>

c中l(wèi)ist的用法

時間: 長思709 分享

  下面小編就跟你們詳細介紹下c中l(wèi)ist的用法的用法,希望對你們有用。

  c中l(wèi)ist的用法的用法如下:

  這幾天在做圖像處理方面的研究,其中有一部分是關(guān)于圖像分割方面的,圖像目標在分割出來之后要做進一步的處理,因此有必要將目標圖像的信息保存在一個變量里面,一開始想到的是數(shù)組,但是馬上就發(fā)現(xiàn)使用數(shù)組的缺點:數(shù)組長度固定,動態(tài)分配內(nèi)存很容易導致錯誤發(fā)生。最重要的一點是我要保存目標圖像的每一點的坐標值,使用數(shù)組就有點無能為力了。因此到百度、Google大神上面找思路,終于被我發(fā)現(xiàn)在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的可變長度的序列元素。這個序列采用雙向鏈表的方式存儲每一個元素,其中每一個元素的數(shù)據(jù)流行都是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操作時,列表的重新分配操作發(fā)生。在這種情況下,只有迭代器或者引用所指向的要刪除的對象的指針變?yōu)闊o效。

  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模板類的主要函數(shù)介紹

  assign() //給list賦值

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

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

  clear() //刪除所有元素

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

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

  erase() //刪除一個元素

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

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

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

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

  merge() //合并兩個list

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

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

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

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

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

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

  remove() //從list刪除元素

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

  resize() //改變list的大小

  reverse() //把list的元素倒轉(zhuǎn)

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

  sort() //給list排序

  splice() //合并兩個list

  swap() //交換兩個list

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

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

  關(guān)于remove和erase函數(shù)

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

  #include <iostream>

  #include <list>

  #include <numeric>

  #include <algorithm>

  using namespace std;

  //創(chuàng)建一個list容器的實例LISTINT

  typedef list<int> TESTINT;

  void main()

  {

  //使用TESTINT創(chuàng)建一個list類型的對象

  TESTINT test;

  //使用TESTINT創(chuàng)建一個迭代器對象

  TESTINT:iterator i;

  //從前面向listOne容器中添加數(shù)據(jù)

  test.push_front (2);

  test.push_front (1);

  //從后面向listOne容器中添加數(shù)據(jù)

  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