list是一个双向列表容器,完成了标准C++数据结构中链表的所有功能;
list与vector和deque类似,只不过其中的对象提供了对元素的随机访问。
STL以双向链表的方式实现list,访问需要从链表的某个端点开始;
list对象插入和删除一个元素所需时间与该元素在链表中的位置无关。
list不提供随机存取(No!),其优势是任何位置插入和删除都非常迅速;
在list中移动一段元素比在vector和deque中要快得多,在需要时可改变自身大小。

C++ Code 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
 
/* stllist.cpp
    list是一个双向列表容器,完成了标准C++数据结构中链表的所有功能;
    list与vector和deque类似,只不过其中的对象提供了对元素的随机访问。
    STL以双向链表的方式实现list,访问需要从链表的某个端点开始;
    list对象插入和删除一个元素所需时间与该元素在链表中的位置无关。
    list不提供随机存取(No!),其优势是任何位置插入和删除都非常迅速;
    在list中移动一段元素比在vector和deque中要快得多,在需要时可改变自身大小。
*/

#include <iostream>
#include <list>
#include <string>

using namespace std;

typedef list<string> LISTSTRING;
typedef list<string>::iterator LISTSTRING_ITER;
typedef list<int>::iterator LISTINT_ITER;

template<class T>
class is_odd
{
public:
    bool operator()(T &val)
    {
        return (val % 2) == 1;
    }

};
int main(void)
{
    //list赋值
    //push_front();
    //push_back();
    //pop_front();
    //pop_back();
    list<int> myList;
    myList.push_back(10);
    myList.push_front(8);

//list大小度量函数
    list<int> c1;
    list<int>::size_type i;
    c1.push_back(23);
    i = c1.size();
    cout << "The c1 size is " << i << endl;
    c1.push_back(24);
    i = c1.size();
    cout << "The c1 size is " << i << endl;
    c1.push_back(25);
    c1.push_back(26);
    c1.resize(5, 50);
    i = c1.size();
    cout << "The c1 size is " << i << endl;
    i = c1.max_size();
    cout << "The c1 max size is " << i << endl;

//list返回函数
    //begin front rbegin 下同
    //end back rend
    int nTmpFront = myList.front();
    int nTmpBack = myList.back();
    cout << nTmpFront << endl;
    cout << nTmpBack << endl;
    //...

//判断是否为空
    if (myList.empty())
    {
        cout << "list is empty!" << endl;
    }

//list元素访问
    //无at  也无operator[]
    LISTSTRING strList;
    strList.push_back("AA");
    strList.push_back("BB");
    strList.push_back("CC");
    strList.push_back("DD");
    LISTSTRING_ITER pListIter;
    for (pListIter = strList.begin(); pListIter != strList.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }

//list重置技术
    LISTSTRING strList1;
    strList1.push_back("AAaa");
    strList1.push_back("BBbb");
    strList1.push_back("CCcc");
    strList1.push_back("DDdd");
    strList.assign(strList1.begin(), strList1.end());
    for (pListIter = strList.begin(); pListIter != strList.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }

//list容器内容交换
    LISTSTRING strListPre;
    strListPre.push_back("AA");
    strListPre.push_back("BB");
    strListPre.push_back("CC");
    strListPre.push_back("DD");
    cout << "strListPre :" << endl;
    for (pListIter = strListPre.begin(); pListIter != strListPre.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }
    LISTSTRING strListPost;
    strListPost.push_back("aa");
    strListPost.push_back("bb");
    strListPost.push_back("cc");
    strListPost.push_back("dd");
    cout << "strListPost :" << endl;
    for (pListIter = strListPost.begin(); pListIter != strListPost.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }
    strListPre.swap(strListPost);
    cout << "strListPre :" << endl;
    for (pListIter = strListPre.begin(); pListIter != strListPre.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }
    cout << "strListPost :" << endl;
    for (pListIter = strListPost.begin(); pListIter != strListPost.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }

//list插入和删除技术
    LISTSTRING listString;
    listString.push_back("Michael Jordan");
    listString.push_back("Kobe Bryant");
    listString.push_back("Lebron James");
    listString.push_back("James Harden");
    for (pListIter = listString.begin(); pListIter != listString.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }
    pListIter = listString.begin();
    listString.insert(pListIter, "Jerry West");
    pListIter++;
    pListIter++;
    listString.insert(pListIter, "Tim Duncan");
    pListIter = listString.end();
    listString.insert(pListIter, "Stephen Curry");
    for (pListIter = listString.begin(); pListIter != listString.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }

//erase操作
    pListIter = listString.begin();
    listString.erase(pListIter);
    cout << "after erase :" << endl;
    for (pListIter = listString.begin(); pListIter != listString.end(); pListIter++)
    {
        cout << *pListIter << endl;
    }

//clear操作
    //erase(list::begin(), list::end())
    list<int> listInt;
    listInt.push_back(97);
    listInt.push_back(98);
    listInt.push_back(99);
    cout << "size of listInt is " << listInt.size() << endl;
    listInt.clear();
    cout << "size of listInt is " << listInt.size() << endl;

//list模板类函数
    // == > >= < <=等等 操作省略

//list特殊函数
    //与vector和deque相比,除了具有以上相同的函数外,还具有一些特殊函数
    //merge remove remove_if sort splice
    list<int> listMerge;
    listMerge.push_back(2);
    listMerge.push_back(4);
    list<int> listMerge1;
    listMerge1.push_back(5);
    listMerge1.push_back(3);
    listMerge.merge(listMerge1);
    listMerge.sort();
    for (LISTINT_ITER pListIntIter = listMerge.begin(); pListIntIter != listMerge.end(); pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }

listInt.push_back(52);
    listInt.push_back(53);
    listInt.push_back(54);
    listInt.remove(53);

for (LISTINT_ITER pListIntIter = listInt.begin();
            pListIntIter != listInt.end();
            pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }
    cout << endl;

list<int> cl1;
    LISTINT_ITER pInt1, pInt2;
    cl1.push_back(1);
    cl1.push_back(2);
    cl1.push_back(3);
    cl1.push_back(4);
    cl1.push_back(5);
    cl1.push_back(6);
    for (LISTINT_ITER pListIntIter = cl1.begin();
            pListIntIter != cl1.end();
            pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }
    cout << endl;
    list<int> cl2 = cl1;
    cl2.remove_if(is_odd<int>());
    for (LISTINT_ITER pListIntIter = cl2.begin();
            pListIntIter != cl2.end();
            pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }
    cout << endl;

//比merge更好用的splice
    cl1.splice(cl1.end(), cl2);
    for (LISTINT_ITER pListIntIter = cl1.begin();
            pListIntIter != cl1.end();
            pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }
    cout << endl;

//保证任意连个连续存储值不同
    list<int> listUnique;
    listUnique.push_back(2);
    listUnique.push_back(2);
    listUnique.push_back(3);
    listUnique.push_back(3);
    listUnique.push_back(5);
    list<int> listUniqueTmp = listUnique;
    for (LISTINT_ITER pListIntIter = listUnique.begin();
            pListIntIter != listUnique.end();
            pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }
    cout << endl;
    //任意两个具有相同值的连续对象的后者删除
    listUnique.unique();
    for (LISTINT_ITER pListIntIter = listUnique.begin();
            pListIntIter != listUnique.end();
            pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }
    cout << endl;
    //把和第一个元素不同的值删除
    not_equal_to<int> mypred;
    listUniqueTmp.unique(mypred);
    for (LISTINT_ITER pListIntIter = listUniqueTmp.begin();
            pListIntIter != listUniqueTmp.end();
            pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }
    cout << endl;

cin.get();
    return 0;
}

STL容器:list双向链表学习相关推荐

  1. 【C++ STL学习之三】容器deque深入学习

    C++ STL容器deque和vector很类似,也是采用动态数组来管理元素. 使用deque之前需包含头文件: #include <deque> 它是定义在命名空间std内的一个clas ...

  2. 【小白学习C++ 教程】二十一、C++ 中的STL容器Arrays和vector

    @Author:Runsen C++的标准模板库(STL)是提供数组.向量.队列等数据结构的模板类的集合.STL是由容器.算法.迭代器组成的库. 容器 容器存储对象和数据.它们基本上是基于模板的泛型类 ...

  3. C++ 笔记(19)— 标准模板库(STL容器、STL迭代器、STL算法、STL容器特点、STL字符串类)

    C++ 标准库可以分为两部分: 标准函数库: 这个库是由通用的.独立的.不属于任何类的函数组成的.函数库继承自 C 语言. 面向对象类库: 这个库是类及其相关函数的集合. C++ 标准库包含了所有的 ...

  4. STL源码剖析学习七:stack和queue

    STL源码剖析学习七:stack和queue stack是一种先进后出的数据结构,只有一个出口. 允许新增.删除.获取最顶端的元素,没有任何办法可以存取其他元素,不允许有遍历行为. 缺省情况下用deq ...

  5. c++ map 修改value_C++知识分享之STL容器:set 容器与 map 容器的简单应用

    set容器中一些函数,取自百度其他大佬已总结好的,如有侵权,请联系删除! set的各成员函数列表如下: c++ stl容器set成员函数:begin()--返回指向第一个元素的迭代器 c++ stl容 ...

  6. c++STL容器的priority_queue

    TL容器的priority_queue STL容器的priority_queue的简介 STL容器的priority_queue的简介 最大值优先级队列.最小值优先级队列 优先级队列适配器 STL ...

  7. c++STL容器的List

    STL容器的List List简介 list对象的默认构造 list头尾的添加移除操作 list的数据存取 list与迭代器 list对象的带参数构造 list的赋值 list的大小 list的插入 ...

  8. STL容器底层数据结构的实现

    C++ STL 的实现: 1.vector      底层数据结构为数组 ,支持快速随机访问 2.list            底层数据结构为双向链表,支持快速增删 3.deque       底层 ...

  9. 用法 stl_C++STL 容器篇

    前言 上一章节主要是详细介绍了C++泛型编程基础,不清楚的可以回顾一下哦.本章节主要针对于C++STL(标准模板类库)做个详细介绍.标准模板类库也就是别人写的模板类,主要内容是各种数据结构的封装,以及 ...

  10. C++知识分享之STL容器:set 容器与 map 容器的简单应用

    set容器中一些函数,取自百度其他大佬已总结好的,如有侵权,请联系删除! set的各成员函数列表如下: c++ stl容器set成员函数:begin()--返回指向第一个元素的迭代器 c++ stl容 ...

最新文章

  1. 卷积网络基础知识---Depthwise Convolution Pointwise Convolution Separable Convolution
  2. 分区文件http://wenku.baidu.com/view/d839d1868762caaedd33d4b7.html
  3. HTML子选择器怎么加图,CSS伪类选择器:before、:after使用:插入字符、插入图片、插入项目编号...
  4. 二叉树的深度_十七:二叉树的最小深度
  5. 【qduoj】C语言课程设计_约瑟夫问题
  6. “光纤之父”高锟辞世!但他的诺奖演讲辞, 青年不可不读!
  7. 一个简单的封ip规则
  8. algorithm:next_permutation
  9. 基于tcpdf将html转成pdf
  10. C ++程序将字符串的每个单词的首字母转换为大写,将其他转换为小写
  11. 微信小程序蓝牙打印(中文乱码已解决)-分包发送(安卓和苹果手机均兼容)
  12. Imbalance data——数据不平衡问题
  13. 粗糙集理论介绍(一)(rough set)
  14. 录屏:mac系统自带功能录制屏幕的方法
  15. 腾讯云账号注册方法介绍
  16. Timingdesigner入门 基础 教程
  17. 「自控元件及线路」4 小功率同步电机
  18. 联系超级计算机,与超级计算机共事是一种怎样的体验?
  19. 对传感器及传感器的应用知多少?
  20. 用JSSE定制SSL连接

热门文章

  1. JS调用ATL DLL
  2. 【OpenCV】图像变换(五)-仿射变换和透视变换
  3. NSGA2 算法Matlab实现
  4. 科学语言与matlab计算 实验2、3
  5. 【机器学习】标准化和归一化辨析
  6. Arcpy 去除shp文件ZM值代码及工具箱
  7. java钝化_session的活化与钝化 (转)
  8. 实习成长之路:操作系统——CPU有哪些工作模式呢?
  9. android andbase,andbase
  10. Mysql同个用户退款订单_微信退款全款退,退一部分,分开退一次以上区别跳坑[订单金额或退款金额与之前请求不一致]...