#include <stdio.h>
#include <list>
#include <algorithm>
using namespace std;
bool RemoveOdd(const int& val)
{
return val % 2;
}
struct Desc : public binary_function<const int&, const int&, bool>
{
result_type operator()(first_argument_type _Left, second_argument_type _Right)
{
return _Left > _Right;
}
};
int main(int argc, char** argv)
{
//创建一个链表
list<int> ilist;
//返回链表大小
printf("size(): %d\n", ilist.size());
//链表最多可以插入的元素个数
printf("max_size(): %d\n", ilist.max_size());
//返回链表是否为空
printf("empty(): %d\n", ilist.empty());
//往链表尾插入元素
ilist.push_back(1);
//往链表头插入元素
ilist.push_front(2);
//往链表尾插入元素
ilist.emplace_back(3);
//往链表头插入元素
ilist.emplace_front(4);
//返回链表尾元素
printf("front(): %d\n", ilist.front());
//返回链表头元素
printf("back(): %d\n", ilist.back());
//返回链表大小
printf("size(): %d\n", ilist.size());
//链表最多可以插入的元素个数
printf("max_size(): %d\n", ilist.max_size());
//返回链表是否为空
printf("empty(): %d\n", ilist.empty());
//从链表尾弹出元素,返回void
ilist.pop_back();
//从链表头返回元素,返回void
ilist.pop_front();
printf("size(): %d\n", ilist.size());
//清空元素
ilist.clear();
ilist.push_back(2);
//往指定pos位置插入元素,返回一个迭代器,执行插入的元素
list<int>::iterator iter = ilist.insert(ilist.begin(), 1);
printf("*iter: %d\n", *iter);
//往指定pos位置的地方插入一个initializer_list,返回一个迭代器指向插入initiazlier_list的第一个元素
iter = ilist.insert(ilist.begin(), { 3, 4, 5 });
printf("*iter: %d\n", *iter);
list<int> ltemp;
ltemp.push_back(6);
//往指定pos位置插入[iter1, iter2)区间内的元素,返回一个迭代器指向插入区间的第一个元素
iter = ilist.insert(ilist.begin(), ltemp.begin(), ltemp.end());
printf("*iter: %d\n", *iter);
//往指定pos位置插入一个元素,返回指向插入元素的迭代器
ilist.emplace(ilist.begin(), 7);
//输出元素
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//链表元素倒序
ilist.reverse();
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//删除指定位置的元素,返回指向下一个位置的迭代器
iter = ilist.erase(ilist.begin());
printf("*iter: %d\n", *iter);
//删除[iter1, iter2)区间的元素,返回下一个位置的迭代器
iter = ilist.erase(ilist.begin(), ilist.end());
if (iter == ilist.end())
printf("end\n");
ilist.push_back(1);
ilist.push_back(2);
ilist.push_back(2);
ilist.push_back(2);
ilist.push_back(1);
//从输入序列中“删除”所有相邻的重复元素,返回void
ilist.unique();
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//移除指定元素,可能移除多个,返回void
ilist.remove(1);
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//移除传入RemoveOdd会返回true的元素,这里也可以定义彷函数作为参数,返回值为void
ilist.remove_if(RemoveOdd);
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
ilist.clear();
ilist.push_back(1);
ilist.push_back(2);
ilist.push_back(2);
ilist.push_back(2);
ilist.push_back(1);
//对链表排序,默认升序,返回void
ilist.sort();
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//对链表排序,降序,返回void
ilist.sort(Desc());
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
list<int> jlist;
jlist.push_back(3);
jlist.push_back(4);
jlist.push_back(5);
ilist.sort(); //都需要降序?奇怪,否则会出错?
//合并列表,去除重复元素
ilist.merge(jlist);
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
ilist.sort(Desc());
jlist.sort(Desc());
ilist.merge(jlist, Desc());
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
ilist.clear();
jlist.clear();
ilist.push_back(1);
ilist.push_back(2);
ilist.push_back(3);
jlist.push_back(2);
jlist.push_back(3);
jlist.push_back(4);
jlist.push_back(5);
//splice实现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list
ilist.splice(ilist.begin(), jlist);
printf("ilist: ");
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
printf("jlist: ");
iter = jlist.begin();
for (; iter != jlist.end(); iter++)
printf("%d ", *iter);
printf("\n");
ilist.clear();
jlist.clear();
ilist.push_back(1);
ilist.push_back(2);
ilist.push_back(3);
jlist.push_back(2);
jlist.push_back(3);
jlist.push_back(4);
jlist.push_back(5);
//splice实现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list
ilist.splice(ilist.begin(), jlist, jlist.begin());
printf("ilist: ");
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
printf("jlist: ");
iter = jlist.begin();
for (; iter != jlist.end(); iter++)
printf("%d ", *iter);
printf("\n");
ilist.clear();
jlist.clear();
ilist.push_back(1);
ilist.push_back(2);
ilist.push_back(3);
jlist.push_back(2);
jlist.push_back(3);
jlist.push_back(4);
jlist.push_back(5);
//splice实现list拼接的功能。将源list的内容部分或全部元素删除,拼插入到目的list
ilist.splice(ilist.begin(), jlist, jlist.begin(), jlist.end());
printf("ilist: ");
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
printf("jlist: ");
iter = jlist.begin();
for (; iter != jlist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//assign函数会把原来list的删除,然后再将[iter1, iter2)的元素插入到list中,返回void
ilist.assign(jlist.begin(), jlist.end());
printf("ilist: ");
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//assign函数会把原来list的删除,然后再将n个m的元素插入到list中,返回void
ilist.assign(3, 2);
printf("ilist: ");
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//assign函数会把原来list的删除,然后再initializer_list插入到list中,返回void
ilist.assign({2, 3, 4});
printf("ilist: ");
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
//交换元素
ilist.swap(jlist);
printf("ilist: ");
iter = ilist.begin();
for (; iter != ilist.end(); iter++)
printf("%d ", *iter);
printf("\n");
printf("jlist: ");
iter = jlist.begin();
for (; iter != jlist.end(); iter++)
printf("%d ", *iter);
printf("\n");
getchar();
return 0;
}
运行结果为:
size(): 0
max_size(): 357913941
empty(): 1
front(): 4
back(): 3
size(): 4
max_size(): 357913941
empty(): 0
size(): 2
*iter: 1
*iter: 3
*iter: 6
7 6 3 4 5 1 2
2 1 5 4 3 6 7
*iter: 1
end
1 2 1
2
2
1 1 2 2 2
2 2 2 1 1
1 1 2 2 2 3 4 5
5 4 3 2 2 2 1 1
ilist: 2 3 4 5 1 2 3
jlist:
ilist: 2 1 2 3
jlist: 3 4 5
ilist: 2 3 4 5 1 2 3
jlist:
ilist:
ilist: 2 2 2
ilist: 2 3 4
ilist:
jlist: 2 3 4