當前位置

首頁 > 商務英語 > 計算機英語 > c中list的用法

c中list的用法

推薦人: 來源: 閱讀: 2.06W 次

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

ing-bottom: 82.5%;">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

rt (n(), 2);

rt (n(), 1);

rt ((), 3);

// 1 2 3

for (i = n(); i != (); ++i)

cout << *i << " ";

cout << endl;

// Insert 3 fours

rt ((), 3, 4);

// 1 2 3 4 4 4

for (i = n(); i != (); ++i)

cout << *i << " ";

cout << endl;

// Insert an array in there

rt ((), rgTest1, rgTest1 + 3);

// 1 2 3 4 4 4 5 6 7

for (i = n(); i != (); ++i)

cout << *i << " ";

cout << endl;

// Insert another LISTINT

rt (n(), rgTest2, rgTest2+3);

rt ((), n(), ());

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

for (i = n(); i != (); ++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容器中添加數據

_front (2);

_front (1);

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

_back (3);

_back (4);

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

i = n();

while(i != ())

{

int tmp = *i;

if(tmp == 2){

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

}else{

i++;

}

}

}