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)
    {
        ;
    }

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

//list大小度量函数
    list<int> c1;
    list<int>::size_type i;
    c1.push_back();
    i = c1.size();
    cout << "The c1 size is " << i << endl;
    c1.push_back();
    i = c1.size();
    cout << "The c1 size is " << i << endl;
    c1.push_back();
    c1.push_back();
    c1.resize();
    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();
    listInt.push_back();
    listInt.push_back();
    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();
    listMerge.push_back();
    list<int> listMerge1;
    listMerge1.push_back();
    listMerge1.push_back();
    listMerge.merge(listMerge1);
    listMerge.sort();
    for (LISTINT_ITER pListIntIter = listMerge.begin(); pListIntIter != listMerge.end(); pListIntIter++)
    {
        cout << *pListIntIter << endl;
    }

listInt.push_back();
    listInt.push_back();
    listInt.push_back();
    listInt.remove();

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();
    cl1.push_back();
    cl1.push_back();
    cl1.push_back();
    cl1.push_back();
    cl1.push_back();
    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();
    listUnique.push_back();
    listUnique.push_back();
    listUnique.push_back();
    listUnique.push_back();
    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();
    ;
}

STL容器:list双向链表学习的更多相关文章

  1. 【STL容器学习】-关联容器与map的用法

    STL提供了4个关联容器:set.multiset.map和multimap.这些容器提供了通过keyword高速存储和訪问数据元素的能力.Set和map不同意有反复keyword,而multiset ...

  2. STL容器与配接器

    STL容器包括顺序容器.关联容器.无序关联容器 STL配接器包括容器配接器.函数配接器 顺序容器: vector                             行为类似于数组,但可以根据要求 ...

  3. STL容器的本质

    http://blog.sina.com.cn/s/blog_4d3a41f40100eof0.html 最近在学习unordered_map里面的散列函数和相等函数怎么写.学习过程中看到了一个好帖子 ...

  4. C++ STL 容器之栈的使用

    Stack 栈是种先进后出的容器,C++中使用STL容器Stack<T> 完美封装了栈的常用功能. 下面来个demo 学习下使用栈的使用. //引入IO流头文件 #include<i ...

  5. 关于STL容器

    容器: 概念:如果把数据看做物体,容器就是放置这些物体的器物,因为其内部结构不同,数据摆放的方式不同,取用的方式也不同,我们把他们抽象成不同的模板类,使用时去实例化它 分类: 序列容器.关联容器.容器 ...

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

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

  7. STL 容器的概念

    STL 容器的概念 在实际的开发过程中,数据结构本身的重要性不会逊于操作于数据结构的算法的重要性,当程序中存在着对时间要求很高的部分时,数据结构的选择就显得更加重要. 经典的数据结构数量有限,但是我们 ...

  8. [转]STL 容器一些底层机制

    1.vector 容器 vector 的数据安排以及操作方式,与 array 非常相似.两者的唯一区别在于空间的运用的灵活性.array 是静态空间,一旦配置了就不能改变,vector 是动态数组.在 ...

  9. STL容器简介

    stl不是面向对象的编程,而是一种不同的编程模式————泛型编程 我们常用到的STL容器有vector.list.deque.map.multimap.set.multiset 顺序性容器:vecto ...

  10. STL容器及算法题:删除奇数的QQ号

    最近思考到这样一个题目:在STL的set和vector容器里存储了1亿个QQ号,编写函数删除奇数QQ号. 1. STL容器简介 首先了解一下 set 和 vector 以及其他类似的 STL 容器: ...

随机推荐

  1. C# 通过form表单下载文本文件

    public void DownLoadConfigFile(string name) { //获取文件字符串内容 var data = _service.ReadFileStr(_configure ...

  2. [翻译] C# 8.0 新特性 Redis基本使用及百亿数据量中的使用技巧分享(附视频地址及观看指南) 【由浅至深】redis 实现发布订阅的几种方式 .NET Core开发者的福音之玩转Redis的又一傻瓜式神器推荐

    [翻译] C# 8.0 新特性 2018-11-13 17:04 by Rwing, 1179 阅读, 24 评论, 收藏, 编辑 原文: Building C# 8.0[译注:原文主标题如此,但内容 ...

  3. ArcGIS 10 安装程序及破解文件

    1.下载 ArcGIS 10 安装程序及破解文件 后面提供电驴的下载地址(可以使用迅雷.QQ旋风等下载工具下载),下载文件是一个光盘镜像文件:‍ArcGIS_Desktop10_122519.iso. ...

  4. Huffman的应用之文件压缩与解压缩

    文件压缩与解压缩>      近期这段时间一直在学习树的这样的数据结构,也接触到了Huffman树以及了解了什仫是Huffman编码,而我们经常使用的zip压缩也是利用的Huffman编码的特性 ...

  5. 深入浅出ObjC之消息 (转)

    在入门级别的ObjC 教程中,我们常对从C++或Java 或其他面向对象语言转过来的程序员说,ObjC 中的方法调用(ObjC中的术语为消息)跟其他语言中的方法调用差不多,只是形式有些不同而已. 譬如 ...

  6. putty自带工具plink自动登陆ssh

    PLINK.EXE -C -N -D 127.0.0.1:7000 root@111.111.111.111  -pw 123456 解释成中文: PLINK.EXE -启用数据压缩 -不要shell ...

  7. python学习之winreg模块

    winreg模块将Windows注册表API暴露给了python. 常见方法和属性 winreg.OpenKey(key,sub_key,reserved = ,access = KEY_READ) ...

  8. linux中使用lftp上传下载文件

    lftp是linux中一款ftp服务器相比windows中的ftp显得要复杂不少了,下面我来总结一下lftp文件上传,文件下载,及文件查找等等相关命令吧. lftp连接的几种方法,最常用的是lftp ...

  9. eclipse 安装maven

    在使用eclipse自带插件的方式安装 http://download.eclipse.org/technology/m2e/releases/ 点击help-->install new sof ...

  10. 安装CentOS版本的yum(转载)

    安装CentOS版本的yum 下载源:http://mirrors.163.com/centos/6/os/i386/Packages/ 材料准备: python-iniparse-0.3.1-2.1 ...