//构造函数
//list()
list
//explicit list(size_type _Count)
list
//list(size_type _Count, const _Ty& _Val)
list
//list(const _Myt& _Right)
list
//list(_Iter _First, _Iter _Last)
int arr[] = { 1, 2, 44, 22, 12 };
list
//添加元素
l5.push_back(3); //1 2 44 22 12 3
//使用迭代器遍历
list
for (iter = l5.begin(); iter != l5.end(); ++iter)
{
cout << *iter << endl; //1 2 44 22 12 3
}
cout << endl;
//反向遍历
list
for (riter = l5.rbegin(); riter != l5.rend(); ++riter)
{
cout << *riter << endl; //3 12 22 44 2 1
}
cout << endl;
//插入元素
l5.push_front(21); //21 1 2 44 22 12 3
l5.insert(++l5.begin(), 11); //21 11 1 2 44 22 12 3
for (iter = l5.begin(); iter != l5.end(); ++iter)
{
cout << *iter << endl; //21 11 1 2 44 22 12 3
}
cout << endl;
//删除元素
l5.pop_front(); //11 1 2 44 22 12 3
l5.pop_back(); //11 1 2 44 22 12
l5.erase(++l5.begin()); //11 2 44 22 12
l5.erase(l5.begin(), ++l5.begin()); //2 44 22 12
for (iter = l5.begin(); iter != l5.end(); ++iter)
{
cout << *iter << endl; //2 44 22 12
}
cout << endl;
//删除所有元素
l5.clear();
//删除所有值为11的元素
l3.remove(11);
for (iter = l3.begin(); iter != l3.end(); ++iter)
{
cout << *iter << endl; //
}
cout << endl;
//交换两个容器
l3.swap(l4);
//list的归并
list
l6.push_back(2);
l6.push_back(13);
l6.push_back(5);
//将l6归并到l3的开始位置之前,l6被清空
l3.splice(l3.begin(), l6);
for (iter = l3.begin(); iter != l3.end(); ++iter)
{
cout << *iter << endl; //2 13 5 11 11 11
}
cout << endl;
l6.push_back(1);
//将l6的开始元素归并到l3的开始位置之前,l6的开始元素被删除
l3.splice(l3.begin(), l6, l6.begin());
for (iter = l3.begin(); iter != l3.end(); ++iter)
{
cout << *iter << endl; //1 2 13 5 11 11 11
}
cout << endl;
//将l6归并到l3,链表预先排好序
l6.push_back(22);
//排序元素
l3.sort();
l3.merge(l6);
for (iter = l3.begin(); iter != l3.end(); ++iter)
{
cout << *iter << endl; //1 2 5 11 11 11 13 22
}
cout << endl;
//剔除连续重复元素
l3.push_back(3);
l3.push_back(3); //1 2 5 11 11 11 13 22 3 3
l3.unique();
for (iter = l3.begin(); iter != l3.end(); ++iter)
{
cout << *iter << endl; //1 2 5 11 13 22 3
}
cout << endl;