list容器介紹
相對於vector容器的連續線性空間,list是一個雙向鏈表,它有一個重要性質:插入操作和刪除操作都不會造成原有的list迭代器失效,每次插入或刪除一個元素就配置或釋放一個元素空間。也就是說,對於任何位置的元素插入或刪除,list永遠是常數時間。
常用函數
(1) 構造函數
list<Elem> c:創建一個空的list
list<Elem> c1(c2):複製另一個同類型元素的list
list<Elem>c(n):創建n個元素的list,每個元素值由默認構造函數確定
list<Elem>c(n,elem):創建n個元素的list,每個元素的值爲elem
list<Elem>c(begin,end):由迭代器創建list,迭代區間爲[begin,end)
(2) 大小、判斷函數
Int size() const:返回容器元素個數
bool empty() const:判斷容器是否爲空,若爲空則返回true
(3) 增加、刪除函數
void push_back(const T& x):list元素尾部增加一個元素x
void push_front(const T& x):list元素首元素錢添加一個元素X
void pop_back():刪除容器尾元素,當且僅當容器不爲空
void pop_front():刪除容器首元素,當且僅當容器不爲空
void remove(const T& x):刪除容器中所有元素值等於x的元素
void clear():刪除容器中的所有元素
iterator insert(iterator it, const T& x ):在迭代器指針it前插入元素x,返回x迭代器指針
void insert(iterator it,size_type n,const T& x):迭代器指針it前插入n個相同元素x
void insert(iterator it,const_iterator first,const_iteratorlast):把[first,last)間的元素插入迭代器指針it前
iterator erase(iterator it):刪除迭代器指針it對應的元素
iterator erase(iterator first,iterator last):刪除迭代器指針[first,last)間的元素
(4) 遍歷函數
iterator begin():返回首元素的迭代器指針
iterator end():返回尾元素之後位置的迭代器指針
reverse_iterator rbegin():返回尾元素的逆向迭代器指針,用於逆向遍歷容器
reverse_iterator rend():返回首元素前一個位置的迭代器指針
reference front():返回首元素的引用
reference back():返回尾元素的引用
(5) 操作函數
void sort():容器內所有元素排序,默認是升序
template<class Pred>void sort(Pred pr):容器內所有元素根據預斷定函數pr排序
void swap(list& str):兩list容器交換功能
void unique():容器內相鄰元素若有重複的,則僅保留一個
void splice(iterator it,list& li):隊列合併函數,隊列li所有函數插入迭代指針it前,x變成空隊列
void splice(iterator it,list& li,iterator first):隊列li中移走[first,end)間元素插入迭代指針it前
void splice(iterator it,list& li,iterator first,iterator last):x中移走[first,last)間元素插入迭代器指針it前
void reverse():反轉容器中元素順序
基本操作示例:
#include "stdafx.h"
#include<iostream>
#include<string>
#include<list>
using namespace std;
typedef list<string> LISTSTR;
int _tmain(int argc, _TCHAR* argv[])
{
<span style="white-space:pre"> </span>LISTSTR test;
<span style="white-space:pre"> </span>test.push_back("back");
<span style="white-space:pre"> </span>test.push_back("middle");
<span style="white-space:pre"> </span>test.push_back("front");
<span style="white-space:pre"> </span>cout<<test.front()<<endl;
<span style="white-space:pre"> </span>cout<<*test.begin()<<endl;
<span style="white-space:pre"> </span>cout<<test.back()<<endl;
<span style="white-space:pre"> </span>cout<<*(test.rbegin())<<endl;
<span style="white-space:pre"> </span>test.pop_front();
<span style="white-space:pre"> </span>test.pop_back();
<span style="white-space:pre"> </span>cout<<test.front()<<endl;
<span style="white-space:pre"> </span>return 0;
}
#include "stdafx.h"
#include<iostream>
#include<string>
#include<list>
using namespace std;
typedef list<int> LISTINT;
int _tmain(int argc, _TCHAR* argv[])
{
LISTINT test;
for(int i=0;i<5;i++)
{
test.push_back(i+1);
}
LISTINT::iterator it = test.begin();
for(;it!=test.end();it++)
{
cout<<*it<<"\t";
}
cout<<endl;
//reverse show
LISTINT::reverse_iterator rit = test.rbegin();
for(;rit!=test.rend();rit++)
{
cout<<*rit<<"\t";
}
cout<<endl;
return 0;
}
#include "stdafx.h"
#include<iostream>
#include<string>
#include<list>
using namespace std;
typedef list<int> LISTINT;
int _tmain(int argc, _TCHAR* argv[])
{
LISTINT test;
test.push_back(1);
test.push_back(5);
test.push_back(3);
test.push_back(10);
LISTINT test2;
test2.push_back(2);
test2.push_back(8);
test2.push_back(6);
test2.push_back(9);
test.sort();
test2.sort();
test.merge(test2);
for(LISTINT::iterator it = test.begin();it!=test.end();it++)
{
cout<<*it<<"\t";
}
cout<<endl;
cout<<test.size()<<"\t"<<test2.size()<<endl;
return 0;
}
上面的代碼展示了sort merge和splice的使用,程序運行結果如下: