特黄特色三级在线观看免费,看黄色片子免费,色综合久,欧美在线视频看看,高潮胡言乱语对白刺激国产,伊人网成人,中文字幕亚洲一碰就硬老熟妇

學(xué)習(xí)啦>學(xué)習(xí)英語(yǔ)>專(zhuān)業(yè)英語(yǔ)>計(jì)算機(jī)英語(yǔ)>

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

時(shí)間: 長(zhǎng)思709 分享

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

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

  這幾天在做圖像處理方面的研究,其中有一部分是關(guān)于圖像分割方面的,圖像目標(biāo)在分割出來(lái)之后要做進(jìn)一步的處理,因此有必要將目標(biāo)圖像的信息保存在一個(gè)變量里面,一開(kāi)始想到的是數(shù)組,但是馬上就發(fā)現(xiàn)使用數(shù)組的缺點(diǎn):數(shù)組長(zhǎng)度固定,動(dòng)態(tài)分配內(nèi)存很容易導(dǎo)致錯(cuò)誤發(fā)生。最重要的一點(diǎn)是我要保存目標(biāo)圖像的每一點(diǎn)的坐標(biāo)值,使用數(shù)組就有點(diǎn)無(wú)能為力了。因此到百度、Google大神上面找思路,終于被我發(fā)現(xiàn)在c++的標(biāo)準(zhǔn)庫(kù)里面還有這么一個(gè)模板類(lèi):list,下面就是對(duì)找到的資料的匯總和加工。

  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.

  本模板類(lèi)描述了一個(gè)對(duì)象,這個(gè)對(duì)象是類(lèi)型為T(mén)的可變長(zhǎng)度的序列元素。這個(gè)序列采用雙向鏈表的方式存儲(chǔ)每一個(gè)元素,其中每一個(gè)元素的數(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.

  對(duì)序列對(duì)象的分配和釋放操作通過(guò)一個(gè)受保護(hù)的對(duì)象allocator進(jìn)行。這樣一個(gè)allocator對(duì)象必須有相同的外部接口作為一個(gè)模板類(lèi)分配器的對(duì)象。注意:當(dāng)對(duì)象被分配之后allocator不能被復(fù)制。

  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.

  當(dāng)一個(gè)成員要進(jìn)行insert或者erase操作時(shí),列表的重新分配操作發(fā)生。在這種情況下,只有迭代器或者引用所指向的要?jiǎng)h除的對(duì)象的指針變?yōu)闊o(wú)效。

  msdn幫助文檔自帶的例子

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

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

  assign() //給list賦值

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

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

  clear() //刪除所有元素

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

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

  erase() //刪除一個(gè)元素

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

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

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

  max_size() //返回list能容納的最大元素?cái)?shù)量

  merge() //合并兩個(gè)list

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

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

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

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

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

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

  remove() //從list刪除元素

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

  resize() //改變list的大小

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

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

  sort() //給list排序

  splice() //合并兩個(gè)list

  swap() //交換兩個(gè)list

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

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

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

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

  #include <iostream>

  #include <list>

  #include <numeric>

  #include <algorithm>

  using namespace std;

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

  typedef list<int> TESTINT;

  void main()

  {

  //使用TESTINT創(chuàng)建一個(gè)list類(lèi)型的對(duì)象

  TESTINT test;

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

  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);

  //從列表中刪除一個(gè)元素

  i = test.begin();

  while(i != test.end())

  {

  int tmp = *i;

  if(tmp == 2){

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

  }else{

  i++;

  }

  }

  }

537205