簡介
List是一種可在常數(shù)時間內(nèi)在任何位置執(zhí)行插入和刪除操作的順序容器。list是雙向鏈表,其迭代器是雙向的。與其他順序容器(array, vector, deque)相比,list容器在任意位置執(zhí)行插入、提取、和移動元素的操作更高效,但它不能通過在容器中的位置直接獲取元素。
成員函數(shù)
復制控制
list::list()
構(gòu)造函數(shù):構(gòu)造一個新的list對象,根據(jù)參數(shù)初始化list容器的內(nèi)容。
list::~list()
析構(gòu)函數(shù):銷毀以list對象。
list::operator=
為容器分配新的內(nèi)容,代替當前的內(nèi)容,隨之修改其大小。
示例代碼
-
#include<iostream>
-
#include<list>
-
-
using namespace std;
-
-
void
-
print(list<int> l)
-
{
-
for(list<int>::iterator it = l.begin(); it != l.end(); ++it)
-
{
-
cout << *it << "";
-
}
-
cout << endl;
-
}
-
-
int
-
main(void)
-
{
-
// list::list()
-
list<int> first;
-
list<int> second(5, 10);
-
list<int> third(second.begin(), second.end());
-
list<int> fourth(third);
-
int arr[] = {1, 2, 3, 4, 5};
-
list<int> fifth(arr, arr + sizeof(arr)/sizeof(int));
-
print(second);
-
print(third);
-
print(fourth);
-
print(fifth);
-
-
// list::operator=
-
first = fifth;
-
print(first);
-
-
return(0);
-
}
Iterator
list::begin()
返回一個迭代器,指向list的第一個元素。返回值類型:iterator/const_iterator。
list::end()
返回一個迭代器,指向list的最后一個元素的下一個位置。返回值類型:iterator/const_iterator。
list::rbegin()
返回一個反轉(zhuǎn)迭代器,指向list的最后一個元素。返回值類型:reverse_iterator/reverse_const_iterator。
list::rend()
返回一個反轉(zhuǎn)迭代器,指向list的第一個元素的前一個位置。返回值類型:reverse_iterator/reverse_const_iterator。
list::cbegin()
begin()的const版本。
list::cend()
end()的const版本
list::crbegin()
rbegin()的cosnt版本。
list::crend()
rend()的const版本。
示例代碼
-
#include <iostream>
-
#include <list>
-
-
using namespace std;
-
-
int
-
main(void)
-
{
-
int arr[] = {1, 2, 3, 4, 5};
-
list<int> l(arr, arr + sizeof(arr)/sizeof(int));
-
-
// list::begin end
-
for(list<int>::iterator it = l.begin(); it != l.end(); ++it)
-
{
-
cout << *it << " ";
-
}
-
cout << endl;
-
-
// list::rbegin rend
-
for(list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it)
-
{
-
cout << *it << " ";
-
}
-
cout << endl;
-
-
// list::cbegin cend
-
for(list<int>::const_iterator it = l.cbegin(); it != l.cend(); ++it)
-
{
-
cout << *it << " ";
-
}
-
cout << endl;
-
-
// list::cbegin cend
-
for(list<int>::const_reverse_iterator it = l.crbegin(); it != l.crend(); ++it)
-
{
-
cout << *it << " ";
-
}
-
cout << endl;
-
-
return 0;
-
}
Capacity
list::empty()
測試list是否為空,空返回true,否則返回false。
list::size()
返回list中元素個數(shù)。返回值類型list::size_type。
list::max_size()
返回list能夠容納元素的最大個數(shù)。返回值類型list::size_type。
示例代碼
-
#include<iostream>
-
#include<list>
-
-
using namespace std;
-
-
int
-
main(void)
-
{
-
list<int> l(10, 20);
-
-
// list::empty
-
if(l.empty()){
-
cout << "Listis empty" << endl;
-
}else{
-
cout << "Listis not empty" << endl;
-
}
-
-
cout << "Listsize : " << l.size() << endl;
-
cout << "Listmax size : " << l.size() << endl;
-
-
return(0);
-
}
Element access
list::front()
返回list第一個元素的引用。返回值類型reference/const_reference。
list::end()
返回list最后一個元素的引用。返回值類型reference/const_reference。
示例代碼
-
#include<iostream>
-
#include<list>
-
-
using namespace std;
-
-
int
-
main(void)
-
{
-
int arr[] = {1, 2, 3, 4, 5};
-
list<int> l(arr, arr + sizeof(arr)/sizeof(int));
-
-
// list::front
-
cout << "Thefirst element is : " << l.front() << endl;
-
// list::back
-
cout << "Thesecond element is : " << l.back() << endl;
-
-
return(0);
-
}
Modifiers
list::assign()
為list分配新的內(nèi)容,以代替當前內(nèi)容,并隨之改變其大小。
list::emplace_front()
在list的前端插入一個元素。插入的元素由其構(gòu)造函數(shù)創(chuàng)建。
list::push_front()
在list的前端插入一個元素。與emplace_front()不同,它的插入方式是將一個已存在的元素復制或移動到list前端。
list::pop_front()
刪除list的第一個元素。
list::emplace_back()
在list的末端插入一個元素。插入的元素由其構(gòu)造函數(shù)創(chuàng)建。
list::push_back()
在list的末端插入一個元素。與emplace_back()不同,它的插入方式是將一個已存在的元素復制或移動到list末端。
list::pop_back()
刪除list的最后一個元素。
list::emplace()
在指定位置插入一個元素。插入的元素由其構(gòu)造函數(shù)創(chuàng)建。
list::insert()
在指定位置插入一個或多個元素。對于插入大量元素來說是非常高效的。
list::erase()
從list中刪除指定位置的一個或一定范圍的元素。
list::swap()
交換兩個list中的元素。兩個list類型必須相同,大小可以不同。
list::resize()
重新分配list的大小,使其能容納指定數(shù)量的元素。
list::clear
刪除list中的所有元素。
示例代碼
-
#include<iostream>
-
#include<list>
-
#include<vector>
-
-
using namespace std;
-
-
void
-
print(list<int> l)
-
{
-
for(list<int>::iterator it = l.begin(); it != l.end(); ++it)
-
{
-
cout << *it << "";
-
}
-
cout << endl;
-
}
-
-
int
-
main(void)
-
{
-
list<int> first;
-
list<int> second;
-
list<int> third;
-
int arr[] = {1, 2, 3, 4, 5};
-
-
// list::assign
-
first.assign(5, 10);
-
second.assign(first.begin(), first.end());
-
third.assign(arr, arr + sizeof(arr)/sizeof(int));
-
print(first);
-
print(second);
-
print(third);
-
-
// list::emplace emplace_front emplace_back
-
list<pair<int, char>> pl;
-
pl.emplace_front(1, 'a');
-
pl.emplace_front(2, 'b');
-
pl.emplace_back(3, 'c');
-
pl.emplace(pl.end(), 4, 'd');
-
for(pair<int, char> x :pl)
-
{
-
cout<< x.first << "" << x.second << endl;
-
}
-
-
//list::push_front push_back
-
third.push_front(10);
-
third.push_back(20);
-
print(third);
-
-
// list::insert
-
first.insert(first.begin(), 20);
-
first.insert(first.end(), 2, 30);
-
vector<int> vec(2, 40);
-
first.insert(first.end(), vec.begin(), vec.end());
-
print(first);
-
-
// list::erase
-
second.erase(second.begin());
-
print(second);
-
second.erase(++second.begin(), second.end());
-
print(second);
-
-
// list::swap
-
second.swap(first);
-
print(first);
-
print(second);
-
-
// list::resize
-
third.resize(5);
-
print(third);
-
third.resize(10);
-
print(third);
-
third.resize(15, 100);
-
print(third);
-
-
// list::clear
-
third.clear();
-
cout << "Thesize of third is : " << third.size() << endl;
-
-
return(0);
-
}
Operations
list::splice()
將一個list A中的元素轉(zhuǎn)移到list B的指定位置,并將A中被轉(zhuǎn)移的元素刪除。
list::remove()
將list中指定的元素刪除。
list::remove_if()
根據(jù)判斷條件刪除list的元素,如果條件為真則刪除該元素。
list::unique()
刪除list中具有相同值的元素,只保留第一個。也可以根據(jù)條件刪除具有相同條件的元素,只保留第一個。
list::merge()
合并兩個list,在合并之前兩個list應該先排序,合并之后的list依然有序。也可以自定義排序的條件。
list::sort()
對list中的元素進行排序,變更它們在容器中的位置。sort()還可以按給定條件進行排序。
list::reverse()
改變list中元素的順序。
示例程序
-
#include<iostream>
-
#include<cmath>
-
#include<list>
-
-
using namespace std;
-
-
void
-
print(list<int> l)
-
{
-
for(list<int>::iterator it = l.begin(); it != l.end(); ++it)
-
{
-
cout << *it << "";
-
}
-
cout << endl;
-
}
-
-
bool
-
single_dight(const int &val)
-
{
-
return val > 10;
-
}
-
-
bool
-
is_near(int first, int second)
-
{
-
return(fabs(first - second) < 5);
-
}
-
-
bool
-
reverse(int first, int second)
-
{
-
return((first -second) > 0);
-
}
-
-
int
-
main(void)
-
{
-
list<int> first(2, 10);
-
list<int> second(2, 20);
-
list<int>::iteratorit;
-
-
// list::splice
-
it = first.begin();
-
first.splice(it, second);
-
print(first);
-
if(second.empty())
-
{
-
cout << "Secondis empty" << endl;
-
}
-
else
-
{
-
cout << "Secondis not empty" << endl;
-
}
-
// it still point to 10(the 3th element)
-
cout << *it << endl;
-
second.splice(second.begin(), first, it);
-
print(second);
-
-
it = first.begin();
-
advance(it, 2);
-
print(first);
-
first.splice(first.begin(), first, it, first.end());
-
print(first);
-
-
// list::remove
-
cout << "Beforeremove : ";
-
print(first);
-
first.remove(10);
-
cout << "Afterremove : ";
-
print(first);
-
-
// list::remove_if
-
cout << "Beforeremove_if : ";
-
first.push_back(10);
-
print(first);
-
first.remove_if(single_dight);
-
cout << "Afterremove_if : ";
-
print(first);
-
-
// list::unique
-
first.push_back(20);
-
first.push_back(20);
-
first.push_back(21);
-
cout << "Beforecall unique() : ";
-
print(first);
-
first.unique();
-
cout << "Aftercall unique() : ";
-
print(first);
-
-
cout << "Beforecall unique() : ";
-
print(first);
-
first.unique(is_near);
-
cout << "Aftercall unique() : ";
-
print(first);
-
-
// list::merge
-
first.push_back(5);
-
first.push_back(12);
-
first.sort();
-
print(first);
-
second.push_back(9);
-
second.push_back(17);
-
second.sort();
-
print(second);
-
-
first.merge(second);
-
print(first);
-
-
// list::sort
-
first.sort(reverse);
-
print(first);
-
-
// list::reverse
-
first.reverse();
-
print(first);
-
return(0);
-
}
|