1、STL简介
C++ STL(标准模板库)是一套功能强大的 C++ 模板类,提供了通用的模板类和函数,这些模板类和函数可以实现多种流行和常用的算法和数据结构,如向量、链表、队列、栈。C++ 标准模板库的核心包括以下三个组件:
这三个组件都带有丰富的预定义函数,帮助我们通过简单的方式处理复杂的任务。
2、list容器原理介绍
STL list 容器,又称双向链表容器,即该容器的底层是以双向链表的形式实现的。这意味着,list 容器中的元素可以分散存储在内存空间里,而不是必须存储在一整块连续的内存空间中。下图展示了双向链表容器是如何存储元素的。
可以看到,list 容器中各个元素的前后顺序是靠指针来维系的,每个元素都配备了 2 个指针,分别指向它的前一个元素和后一个元素。其中第一个元素的前向指针总为 null,因为它前面没有元素;同样,尾部元素的后向指针也总为 null。
3、list容器使用介绍
list 容器以模板类 list(T 为存储元素的类型)的形式在头文件中,并位于 std 命名空间中。因此在使用list容器之前需要包含list容器头文件。
#include <list>
(1)创建list容器
下面创建的是一个元素类型是int的list。
std::list<int> var;
(2)list成员函数
-
迭代器函数 (Iterators)
迭代器函数共有8个,其中后面四个函数是C++11标准新增。 -
容量函数(Capacity)
-
元素访问函数(Element access)
-
修改函数(Modifiers)
其中emplace_front 、emplace_back 和 emplace 是C++11标准新增函数。 -
操作函数(Operations)
4、list使用实例
- 使用迭代器遍历 list 容器中的各个元素。
#include <iostream>
#include <list>
using namespace std;
int main()
{
//创建 list 容器
std::list<char> values{'h','t','t','p',':','/','/','c','.','b','i','a','n','c','h','e','n','g','.','n','e','t'};
//使用begin()/end()迭代器函数对输出list容器中的元素
for (std::list<char>::iterator it = values.begin(); it != values.end(); ++it) {
std::cout << *it;
}
cout << endl;
//使用 rbegin()/rend()迭代器函数输出 lsit 容器中的元素
for (std::list<char>::reverse_iterator it = values.rbegin(); it != values.rend();++it) {
std::cout << *it;
}
return 0;
}
- 基本插入、接合等操作:
#include <iostream>
#include <list>
using namespace std;
int main()
{
//创建 list 容器
std::list<char> values{'h','t','t','p',':','/','/','c','.','b','i','a','n','c','h','e','n','g','.','n','e','t'};
//创建 begin 和 end 迭代器
std::list<char>::iterator begin = values.begin();
std::list<char>::iterator end = values.end();
//头部和尾部插入字符 '1'
values.insert(begin, '1');
values.insert(end, '1');
while (begin != end)
{
std::cout << *begin;
++begin;
}
return 0;
}
- push等操作
#include <iostream>
#include <list>
#include <numeric>
#include <algorithm>
#include <windows.h>
using namespace std;
typedef list<int> LISTINT;
typedef list<int> LISTCHAR;
void main()
{
//用LISTINT创建一个list对象
LISTINT listOne;
//声明i为迭代器
LISTINT::iterator i;
listOne.push_front(3);
listOne.push_front(2);
listOne.push_front(1);
listOne.push_back(4);
listOne.push_back(5);
listOne.push_back(6);
cout << "listOne.begin()--- listOne.end():" << endl;
for (i = listOne.begin(); i != listOne.end(); ++i)
cout << *i << " ";
cout << endl;
LISTINT::reverse_iterator ir;
cout << "listOne.rbegin()---listOne.rend():" << endl;
for (ir = listOne.rbegin(); ir != listOne.rend(); ir++) {
cout << *ir << " ";
}
cout << endl;
int result = accumulate(listOne.begin(), listOne.end(), 0);
cout << "Sum=" << result << endl;
cout << "------------------" << endl;
//用LISTCHAR创建一个list对象
LISTCHAR listTwo;
//声明i为迭代器
LISTCHAR::iterator j;
listTwo.push_front('C');
listTwo.push_front('B');
listTwo.push_front('A');
listTwo.push_back('D');
listTwo.push_back('E');
listTwo.push_back('F');
cout << "listTwo.begin()---listTwo.end():" << endl;
for (j = listTwo.begin(); j != listTwo.end(); ++j)
cout << char(*j) << " ";
cout << endl;
j = max_element(listTwo.begin(), listTwo.end());
cout << "The maximum element in listTwo is: " << char(*j) << endl;
Sleep(10000);
}
输出结果:
listOne.begin()--- listOne.end():
1 2 3 4 5 6
listOne.rbegin()---listOne.rend():
6 5 4 3 2 1
listTwo.begin()---listTwo.end():
A B C D E F
参考资料:
《https://www.cplusplus.com/reference/list/list/》
《http://c.biancheng.net/view/6892.html》
《http://c.biancheng.net/view/6922.html》
《https://blog.csdn.net/fanyun_01/article/details/56881515》