Reprinted from: http://hi.baidu.com/xuehuo_0411/blog/item/6993c7084a8987a12fddd42f.html
Summary:
list is a doubly linked list of generic container, which data elements can be cascaded into a linear linked list pointer table logical sense. Unlike vector and deque container using a linear sequence of memory structure table, list elements in any two-way linked list to find a position, insertions and deletions having both high order algorithm constant time complexity of O (1).
list Application Foundation:
Create a list objects:
1, list (const A & a = A ()) Create an empty list object.
如:list<int> l;
2, list (size_type n) to create a list of objects having n elements, each list element has a default value of its type.
如:list<int> l(10);
3, list (size_type n, const T & value) to create a list of objects having n elements, each element having a value of an initial value.
如:list<int> l(10, 5);
4, list (const list &) by copying a value of each element of a list object, creating a new list object.
如:list<int> l1(10, 5); list<int> l2(l1);
5, list (const InputIterator first, const InputIterator last, const A & a = A ()) by copying the iterator range [first, last) element values, creating a new object list, the memory allocator can be omitted.
如:int iArray[] = {1,2,3}; list<int> l(iArray, iArray+3);
Initialization assignment:
push_back function list to provide a list of commonly initialization container, inserting a new element push_back function value at the end of the container.
void push_back(const T& value)
Access to traverse elements:
Since the data in the linked list requires a traversing elements, thus traversing manner only list element iterator.
Insert elements:
Since the list inserted list elements do not need to copy the other shifting elements, therefore, list element into (1) the complexity of the algorithm is O function has a constant order. In addition push_back function to add elements to the end outside, list elements there are first inserted in the chain and function push_front inserted at any position iterator insert function.
iterator insert(iterator pos, const T& x)
void push_front(const T&)
Delete elements:
iterator erase (iterator pos) deletes the list of elements within the meaning of the iterator pos
iterator erase (iterator first, iterator last) delete the iterator range [first, last) list of all the elements
void clear () deletes all list elements
void pop_front () delete the list of the first list element
void pop_back () delete the list of the last element of a list
void remove (const T & value) of all elements in the list to delete list element value of the value
Reverse traversal elements:
reverse_iterator rbegin()
reverse_iterator rend()
list of exchange:
swap function list container by simply swapping the two head pointer list, the list element to achieve the exchange.
void swap(list&)
list of merge:
void splice(iterator position, list& x) 将x的链表归并到当前list链表的position位置之前,list对象x将被清空。
void splice(iterator position, list&, iterator i) 将一个list的迭代器i值所指的元素,归并到当前list链表中,并将归并的元素从原链表中删除。
void merge(list& x) 将list对象x的链表归并到当前list链表中,并清空x链表。只有当前链表和被归并的x链表的元素均预先按元素值的"<"关系排好序,merge函数才具有意义,归并后的链表元素也是按"<"关系排序的。
list的元素排序:
list提供的void sort函数,按"<"关系进行list链表的元素排序,较小的元素排在前面。
list的连续重复元素的删除:
利用list的void unique函数,可将连续重复的元素删除,仅保留一个。
举例分析:
1、
//插入list链表元素
#include <list>
#include <iostream>
using namespace std;
int main(void)
{
list<int> l;
l.push_back(6);
l.push_back(8);
l.push_back(9);
//插入链表元素
list<int>::iterator i, iend;
i = l.begin();
i++;
l.insert(i, 7);
l.push_front(5);
//打印链表元素
iend = l.end();
for (i=l.begin(); i!=iend; i++)
cout << *i << " ";
return 0;
}
2、
//list链表元素的删除
#include <list>
#include <iostream>
using namespace std;
int main(void)
{
list<int> l;
l.push_back(5);
l.push_back(6);
l.push_back(7);
l.push_back(8);
l.push_back(9);
l.push_back(9);
l.push_back(9);
l.push_back(10);
//删除元素,剩下7、8和9
list<int>::iterator i, iend;
i = l.begin();
i++;
l.erase(i);
l.pop_back();
l.pop_front();
l.remove(9);
//打印
iend = l.end();
for (i=l.begin(); i!=iend; i++)
{
cout << *i << " ";
}
return 0;
}
3、
//list链表的归并
#include <list>
#include <iostream>
using namespace std;
void print(list<int>& l)
{
list<int>::iterator i, iend;
iend = l.end();
for (i=l.begin(); i!=iend; i++)
{
cout << *i << " ";
}
}
int main(void)
{
list<int> l;
for (int j=1; j<=10; j++)
{
l.push_back(j);
}
//splice()函数
list<int> carry;
carry.splice(carry.begin(), l, l.begin());
//打印carry
cout << "carry的链表元素为: ";
print(carry);
cout << endl;
//打印l
cout << "l的链表元素为: ";
print(l);
cout << endl;
//merge()函数用法
list<int> x;
x.push_back(30);
x.push_back(31);
x.push_back(32);
l.merge(x);
//打印x
cout << "x的链表元素为: 空";
print(x);
cout << endl;
//打印l
cout << "l的链表元素为: ";
print(l);
cout << endl;
return 0;
}
4、
//list链表元素的排序
#include <list>
#include <iostream>
using namespace std;
void print(list<int>& l)
{
list<int>::iterator i, iend;
iend=l.end();
for (i=l.begin(); i!=iend; i++)
cout << *i << " ";
cout << endl;
}
int main(void)
{
list<int> l;
for(int j=18; j>=0; j--)
l.push_back(j);
cout << "排序前: ";
print(l);
//调用list<int>::sort()函数排序
l.sort();
cout << "排序后: ";
print(l);
return 0;
}
5、
//list连续重复元素的删除
#include <list>
#include <iostream>
using namespace std;
int main(void)
{
list<int> l;
l.push_back(6);
l.push_back(8);
l.push_back(6);
l.push_back(6);
l.push_back(6);
l.push_back(9);
l.push_back(13);
l.push_back(6);
l.unique();
list<int>::iterator i, iend;
iend = l.end();
for (i=l.begin(); i!=iend; i++)
cout << *i << " ";
cout << endl;
return 0;
}
转载于:https://www.cnblogs.com/yang3wei/archive/2012/04/17/2739779.html