构造函数

list<int> c0; //空链表
list<int> c1(); //建一个含三个默认值是0的元素的链表
list<int> c2(,); //建一个含五个元素的链表,值都是2
list<int> c4(c2); //建一个c2的copy链表
list<int> c5(c1.begin(),c1.end()); ////c5含c1一个区域的元素[_First, _Last)。

成员函数

c.begin() 返回指向链表第一个元素的迭代器。
c.end() 返回指向链表最后一个元素之后的迭代器。

list<int> a1{,,,,};
list<int>::iterator it;
for(it = a1.begin();it!=a1.end();it++){
cout << *it << "\t";
}
cout << endl;

c.rbegin() 返回逆向链表的第一个元素,即c链表的最后一个数据。

c.rend() 返回逆向链表的最后一个元素的下一个位置,即c链表的第一个数据再往前的位置。

list<int> a1{,,,,};
list<int>::reverse_iterator it;
for(it = a1.rbegin();it!=a1.rend();it++){
cout << *it << "\t";
}
cout << endl;

operator= 重载赋值运算符。

list<int> a1 {,,,,},a2;
a2 = a1;
list<int>::iterator it;
for(it = a2.begin();it!=a2.end();it++){
cout << *it << endl;
}

c.assign(n,num) 将n个num拷贝赋值给链表c。

c.assign(beg,end) 将[beg,end)区间的元素拷贝赋值给链表c。

int a[] = {,,,,};
list<int> a1;
list<int>::iterator it;
a1.assign(,);
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.assign(a,a+);
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

c.front() 返回链表c的第一个元素。
c.back() 返回链表c的最后一个元素

list<int> a1{,,,,};
if(!a1.empty()){
cout << "the first number is:" << a1.front() << endl;
cout << "the last number is:" << a1.back() << endl;
}

c.empty() 判断链表是否为空。

list<int> a1{,,,,};
if(!a1.empty())
cout << "a1 is not empty" << endl;
else
cout << " a1 is empty" << endl;

c.size() 返回链表c中实际元素的个数。

c.max_size() 返回链表c可能容纳的最大元素数量。
c.clear() 清除链表c中的所有元素。
c.insert(pos,num) 在pos位置插入元素num。
c.insert(pos,n,num) 在pos位置插入n个元素num。
c.insert(pos,beg,end) 在pos位置插入区间为[beg,end)的元素。

list<int> a1{,,,,};
list<int>::iterator it;
cout << "insert before:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.insert(a1.begin(),);
cout << "insert(pos,num) after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.insert(a1.begin(),,);
cout << "insert(pos,n,num) after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
int arr[] = {,,,,};
a1.insert(a1.begin(),arr,arr+);
cout << "insert(pos,beg,end) after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

c.erase(pos)    删除pos位置的元素。

list<int> a1{,,,,};
list<int>::iterator it;
cout << "erase before:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.erase(a1.begin());
cout << "erase after:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

c.push_back(num) 在末尾增加一个元素。
c.pop_back() 删除末尾的元素。
c.push_front(num) 在开始位置增加一个元素。
c.pop_front() 删除第一个元素。

list<int> a1{,,,,};
a1.push_back();
list<int>::iterator it;
cout << "push_back:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.pop_back();
cout << "pop_back:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.push_front();
cout << "push_front:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.pop_front();
cout << "pop_front:";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

resize(n) 从新定义链表的长度,超出原始长度部分用0代替,小于原始部分删除。
resize(n,num) 从新定义链表的长度,超出原始长度部分用num代替。

list<int> a1{,,,,};
a1.resize();
list<int>::iterator it;
cout << "resize(n):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a1.resize(, );
cout << "resize(n,num):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

c1.swap(c2); 将c1和c2交换。
swap(c1,c2); 同上

list<int> a1{,,,,},a2,a3;
a2.swap(a1);
list<int>::iterator it;
cout << "a2.swap(a1):";
for(it = a2.begin();it!=a2.end();it++){
cout << *it << " ";
}
cout << endl; swap(a3,a2);
cout << "swap(a3,a2):";
for(it = a3.begin();it!=a3.end();it++){
cout << *it << " ";
}

c1.merge(c2) 合并2个有序的链表并使之有序,从新放到c1里,释放c2。
c1.merge(c2,comp) 合并2个有序的链表并使之按照自定义规则排序之后从新放到c1中,释放c2。

list<int> a1{,,},a2{,,};
a1.merge(a2);
list<int>::iterator it;
cout << "a1.merge(a2):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;
a2.merge(a1,[](int n1,int n2){return n1>n2;});
cout << "a2.merge(a1,comp):";
for(it = a2.begin();it!=a2.end();it++){
cout << *it << " ";
}
cout << endl;

c1.splice(c1.beg,c2) 将c2连接在c1的beg位置,释放c2

ist<int> a1{,,},a2{,,};
a1.splice(a1.begin(), a2);
list<int>::iterator it;
cout << "a1.merge(a2):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

c1.splice(c1.beg,c2,c2.beg) 将c2的beg位置的元素连接到c1的beg位置,并且在c2中施放掉beg位置的元素

list<int> a1{,,},a2{,,};
a1.splice(a1.begin(), a2,++a2.begin());
list<int>::iterator it;
cout << "a1.merge(a2):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

c1.splice(c1.beg,c2,c2.beg,c2.end) 将c2的[beg,end)位置的元素连接到c1的beg位置并且释放c2的[beg,end)位置的元素

list<int> a1{,,},a2{,,};
a1.splice(a1.begin(),a2,a2.begin(),a2.end());
list<int>::iterator it;
cout << "a1.merge(a2):";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

remove(num) 删除链表中匹配num的元素。

list<int> a1{,,,,};
a1.remove();
list<int>::iterator it;
cout << "remove():";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

remove_if(comp) 删除条件满足的元素,参数为自定义的回调函数。

list<int> a1{,,,,};
a1.remove_if([](int n){return n<;});
list<int>::iterator it;
cout << "remove_if():";
for(it = a1.begin();it!=a1.end();it++){
cout << *it << " ";
}
cout << endl;

reverse() 反转链表
unique() 删除相邻的元素
c.sort() 将链表排序,默认升序
c.sort(comp) 自定义回调函数实现自定义排序

重载运算符
operator==
operator!=
operator<
operator<=
operator>
operator>=

STL之list使用简介的更多相关文章

  1. STL之容器(containers) 简介

    什么是容器? 容器用来存储数据的,数据可以是用户自定义类型(对象),也可以是预定义类型,c++中的容器主要使用如vector,list (顺序容器) 这些都是已经封装好了. 1.结构(struct): ...

  2. stl空间配置器简介

    1. 符合STL标准的空间配器接口 STL是c++中使用非常广泛的一个标准库,它包含各种有用的容器.而空间配置器作为STL各种容器的背后的核心,负责容器内部内存的分配和释放.不过空间配置器可以分配的也 ...

  3. STL之heap使用简介

    STL中并没有把heap作为一种容器组件,heap的实现亦需要更低一层的容器组件(诸如list,array,vector)作为其底层机制.Heap是一个类属算法,包含在algorithm头文件中.虽然 ...

  4. STL之priority_queue使用简介

    优先队列容器也是一种从一端入队,另一端出对的队列.不同于一般队列的是,队列中最大的元素总是位于队首位置,因此,元素的出对并非按照先进先出的要求,将最先入队的元素出对,而是将当前队列中的最大元素出对. ...

  5. STL之vector使用简介

    Vector成员函数 函数 表述 c.assign(beg,end)c.assign(n,elem) 将[beg; end)区间中的数据赋值给c.将n个elem的拷贝赋值给c. c.at(idx) 传 ...

  6. STL之string使用简介

    声明一个C++字符串 string类的构造函数和析构函数如下: string s; //生成一个空字符串s string s(str) //拷贝构造函数 生成str的复制品 string s(str, ...

  7. STL之算法使用简介

    accumlate : iterator 对标志的序列中的元素之和,加到一个由 init 指定的初始值上.重载的版本不再做加法,而是传进来的二元操作符被应用到元素上.  adjacent_differ ...

  8. STL之deque使用简介

    deque函数列表 函数 c.assign(beg,end)c.assign(n,elem) c.at(idx) c.back() c.begin() c.clear() deque<Elem& ...

  9. C++ Standard Template Library STL(undone)

    目录 . C++标准模版库(Standard Template Library STL) . C++ STL容器 . C++ STL 顺序性容器 . C++ STL 关联式容器 . C++ STL 容 ...

随机推荐

  1. 20145238-荆玉茗 《Java程序设计》第9周学习总结

    20145238第九周<Java学习笔记> 第十六章 整合数据库 JDBC入门 ·数据库本身是个独立运行的应用程序 ·撰写应用程序是利用通信协议对数据库进行指令交换,以进行数据的增删查找 ...

  2. SQL Server 2008 R2 附加数据库 “尝试打开或创建物理文件 拒绝访问”的解决办法

    其实是来自一篇SQL Server 2005同样错误的帖子,不过试了在SQL Server 2008 R2下面也有效,记录一下. 解决方法: 在所有程序—Microsoft SQL Server 20 ...

  3. 【iOS】史上最全的iOS持续集成教程 (下)

    :first-child{margin-top:0!important}.markdown-body>:last-child{margin-bottom:0!important}.markdow ...

  4. mysql存储问题

    为什么标题要起这个名字呢?commen sence指的是那些大家都应该知道的事情,但往往大家又会会略这些东西,或者对这些东西一知半解,今天我总结下自己在mysql中遇到的一些commen sense类 ...

  5. 获取Grid后台动态添加的子项

    例:Grid的子项是包含边框的复选框CheckBox //遍历Grid中的子项 foreach (var c in this.grid_box.Children) { Border bd = c as ...

  6. Java OOP——第七章 多线程

    1.进程:是指运行中的应用程序,每个进程都有自己独立的地址空间(内存空间): Eg:用户点击桌面的IE浏览器,就启动了一个进程,操作系统就会为该进程分配独立的地址空间.当用户再次点击左面的IE浏览器, ...

  7. 在程序开发中,++i 与 i++的区别

    在不参与运算的情况下,i++和++i都是在变量的基础加1 ◆在参与运算的情况下 Var i=123; Var j=i++;  先将i的值123赋值给j,之后再自增 j的值为123  i 的值为124 ...

  8. 【转载】最长回文字符串(manacher算法)

    原文转载自:http://blog.csdn.net/lsjseu/article/details/9990539 偶然看见了人家的博客发现这么一个问题,研究了一下午, 才发现其中的奥妙.Stupid ...

  9. redis源代码结构解析

    看了黄建宏老师的<Redis设计与实现>,对redis的部分实现有了一个简明的认识: 之前面试的时候被问到了这部分的内容,没有关注,好在还有时间,就把Redis的源码看了一遍. Redis ...

  10. 关于json输出为null?

    原因: 该字符中含了ASCII码ETB控制符,即\x17导致json解析失败   解决方案: $params = preg_replace('/[\x00-\x1F]/', '', $params); ...