List 就是一雙向鏈表,可高效地進行插入刪除元素。包括構造、方法等。內存空間可以是不連續的,通過指針來進行數據的訪問
優: 1)插入/刪除效率高
缺: 1)不支持隨機存取,查詢效率較低
在使用list必須包括頭文件#include <list>
構造:
list<int> l1; //創建一個沒有任何元素的list
list<int> l2(10); //創建一個有10個元素的list,每個元素值爲默認
list<double> l3(10, 9.3); //創建具有10個元素的list,每個元素的初始值爲9.3
list<double> l4(l3); //通過拷貝一個list對象的元素,創建一個新的list對象
int iArray[] ={3, 10, 19};
list<int> l5(iArray, iArray + 3);//將另一個list對象的迭代器區間[first, last)所指的元素,拷貝到新創建的list對象中
成員函數:
assign | assign elements to a list |
back | returns a reference to last element of a list |
begin | returns an iterator to the beginning of the list |
clear | removes all elements from the list |
empty | true if the list has no elements |
end | returns an iterator just past the last element of a list |
erase | removes elements from a list |
front | returns a reference to the first element of a list |
insert | inserts elements into the list |
max_size | returns the maximum number of elements that the list can hold |
merge | merge two lists |
pop_back | removes the last element of a list |
pop_front | removes the first element of the list |
push_back | add an element to the end of the list |
push_front | add an element to the front of the list |
rbegin | returns a reverse_iterator to the end of the list |
remove | removes elements from a list |
remove_if | removes elements conditionally |
rend | returns a reverse_iterator to the beginning of the list |
resize | change the size of the list |
reverse | reverse the list |
size | returns the number of items in the list |
sort | sorts a list into ascending order |
splice | merge two lists in constant time |
swap | swap the contents of this list with another |
unique | removes consecutive duplicate elements |
中文列表:
assign() //分配值,有兩個重載:
c1.assign(++c2.begin(), c2.end()) //c1現在爲(50,60)。
c1.assign(7,4) //c1中現在爲7個4,c1(4,4,4,4,4,4,4)。
back() //返回最後一元素的引用:
begin() //返回第一個元素的指針(iterator)
clear() //刪除所有元素
empty() //判斷是否鏈表爲空
end() //返回最後一個元素的下一位置的指針(list爲空時end()=begin())
erase() //刪除一個元素或一個區域的元素(兩個重載)
front() //返回第一個元素的引用:
insert() //在指定位置插入一個或多個元素(三個重載):
max_size() //返回鏈表最大可能長度(size_type就是int型):
merge() //合併兩個鏈表並使之默認升序(也可改):
pop_back() //刪除鏈表尾的一個元素
pop_front() //刪除鏈表頭的一元素
push_back() //增加一元素到鏈表尾
push_front() //增加一元素到鏈表頭
rbegin() //返回鏈表最後一元素的後向指針(reverse_iterator or const)
rend() //返回鏈表第一元素的下一位置的後向指針
remove() //刪除鏈表中匹配值的元素(匹配元素全部刪除)
remove_if() //刪除條件滿足的元素(會遍歷一遍鏈表)
resize() //重新定義鏈表長度(兩重載):
reverse() //反轉鏈表:
size() //返回鏈表中元素個數
sort() //對鏈表排序,默認升序(可自定義)
splice() //對兩個鏈表進行結合(三個重載)
swap() //交換兩個鏈表(兩個重載)
unique() //刪除相鄰重複元素(斷言已經排序,因爲它不會刪除不相鄰的相同元素)
使用:
1)、如何定義一個list對象
#include <list>
int main (void)
{
list<char > cList; //聲明瞭list<char>模板類 的一個實例
}
2)、使用list的成員函數push_back和push_front插入一個元素到list中
cList. push_back('a'); //把一個對象放到一個list的後面
cList. push_front ('b'); //把一個對象放到一個list的前面
3)、使用list的成員函數empty()判斷list是否爲空
if (cList.empty())
{
printf("this list is empty");
}
4)、用list< char >::iterator得到指向list的指針
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支持隨機存取。
5)、用STL的通用算法count()來統計list中的元素個數
int cNum;
char ch = 'b';
cNum = count(cList.Begin(), cList.end(), ch); //統計list中的字符b的個數
說明:在使用count()函數之前必須加入#include <algorithm>
6)、用STL的通用算法count_if ()來統計list中的元素個數
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()更加複雜的評估以確定一個對象是否應該被記數。
7)、使用STL通用算法find()在list中查找對象
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的指針。
8)、使用STL通用算法find_if()在list中查找對象
const char c('c');
class IsC
{
public:
bool operator() ( char& ch )
{
return ch== c;
}
};
list<char>::iterator FindIterator
FindIterator = find_if (cList.begin(), cList.end(),IsC());//查找字符串c;
說明:如果沒有找到指定的對象,就會返回cList.end()的值,找到了就返回一個指向對象iterator的指針。
9)、使用list的成員函數sort()排序
cList.sort();
10)、使用list的成員函數insert插入一個對象到list中
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指出的位置以前。
11)、如何在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');
用list實現堆棧:
#ifndef LL_STACK
#define LL_STACK
#include <list>
template<class T>
class LLStack
{
public:
LLStack()//構造函數
{
}
void clear()//清空棧
{
lst.clear();//清空容器
}
bool isEmpty() const//判斷棧是否爲空
{
return lst.empty();//容器是否爲空
}
T& topEl()//取棧頂元素
{
return lst.back();//取容器最後一個元素
}
T pop()//出棧
{
T el = lst.back();//取容器最後一個元素
lst.pop_back();//刪除最後一個元素
return el;
}
void push(const T& el)//進棧
{
lst.push_back(el);//在容器的最末尾增加一個元素
}
private:
list<T> lst;
};
#endif