关于STL你知道多少?
引入:
有的时候,你要在程序中用到堆、栈、队列、链表等一些基本的算法,而你又实在不想自己去实现数据结构教科书中那些繁琐的算法,那么你就可以考虑使用STL。
介绍: STL(Standard Template Library),即标准模板库,是一个具有工业强度的,高效的C++程序库。
STL是一个template程序库。它对容器,迭代器,算法以及函数对象的规约有极佳的紧密配合与协调。STL有自己特殊的办事方法,当你伙同STL一起工作时,必须遵守它的规约。
六大组件
(1)容器:包含一组元素的对象。
顺序容器:容器中元素的在容器中的存储位置随着元素进入容器的时间和地点而改变。
vector(向量),deque(双端队列),list(列表),stack(栈),queue(队列)
关联容器:set(集合),multiset(多重集合),map(映射),multimap(多重映射)
(2)算法:可以广泛用于不同的对象和内置的数据类型。
常用算法:sort,search,copy,erase。
使用STL的算法,需要包含头文件<algorithm>
(3)迭代器:提供了顺序访问容器中的每个元素的方法,扮演容器与算法之间的胶合剂——“泛型编程”。
是一种将operator*,operator->,operator++,operator–等指针相关操作予以重载的class template。
可以使用“++”运算符来获得指向下一个元素的迭代器;可以使用“*”运算符访问迭代器所指向的元素,如果元素类型是类或者结构体,还可以使用“->”运算符直接访问该元素的一个成员。
迭代器是泛化的指针:指针也具有同样的特性,因此指针本身就是一种迭代器。
使用独立于STL容器的迭代器,需要包含头文件<iterator>。
(4)仿函数:可作为算法的某种策略。
仿即类似的意思,在这里指一个行为类似函数的对象,它可以没有参数,也可以带有若干参数。任何重载了调用运算符operator()的类的对象都满足函数对象的特征
(5)适配器:一种用来修饰容器或仿函数或迭代器接口的东西。
适配器是一种接口类为已有的类提供新的接口目的是简化、约束、使之安全、隐藏或者改变被修改类提供的服务集合。
(6)配置器:负责空间配置与管理。
从实现的角度来看,配置器是一个实现了动态空间配置、空间管理、空间释放的class template。
六大组件的交互关系图:(下图来自《STL原码剖析》)
STL中常用容器有:list,vecto,rdeque,set,map,接下来我们来学习list,其它容器以后会依次介绍。
list
List是一个序列容器,可以说是我们平常所写的链表,但是不完全相同,库里的list是一个用模板实现的双向循环链表。除此之外还包括迭代器的一些操作。
begin():它返回的是头结点的下一个结点。因为库里的list是带头结点的链表。
end(): 它返回的是头结点,也就是说返回尾结点的下一个结点。
rbegin()和rend():是逆序后对应的节点。
相关接口和操作:
接下来练习一些list常见的用法。
(1)resize: 重新定义链表长度,并用默认值0填补未初始化的位。
#include<list>
int test()
{
list<int> l;
for(int i = 1;i <= 10 ;++i)
{
l.push_back(i);
}
l.resize(5);
l.resize(8,10);
l.resize(12);
for (list<int>::iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
}
(2)reverse:反转链表
#include<list>
void test1()
{
list<int> l;
for(int i = 1;i <= 6;++i)
{
l.push_back(i);
}
cout<<"list容器的元素: ";
for (list<int>::iterator it= l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
cout<<"逆置后的元素: ";
for (list<int>::reverse_iterator it = l.rbegin(); it != l.rend(); ++it)
{
cout << *it << " ";
}
cout << endl;
}
(3)insert:在指定位置插入一个或多个元素
void test2()
{
list<int> l;
list<int>::iterator it;
for(int i = 1;i <= 6;++i)
{
l.push_back(i);
}
it = l.begin();
l.insert(it,10);//10 1 2 3 4 5 6
cout<<"头插数字10: ";
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
++it;
l.insert(it,2,20);//10 1 20 20 2 3 4 5 6
cout<<"在2前插入两个20: ";
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
}
(4)erase:删除一个元素或一个区域的元素
#include<list>
void test3()
{
list<int> l;
list<int>::iterator it,it1;
for(int i = 1;i <= 6;++i)
{
l.push_back(i);
}
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
it = it1 = l.begin();
advance(it1,3);
l.erase(it);
cout<<"删除第一个元素: ";
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
cout << endl;
l.erase(it1);
cout<<"删除第三个元素: ";
for (list<int>::iterator it = l.begin(); it != l.end(); it++)
{
cout << *it << " ";
}
}
(5)remove:删除链表中匹配值的元素(匹配元素全部删除)
void test4()
{
list<int> l;
l.push_back(1);
l.push_back(2);
l.push_back(1);
l.push_back(4);
l.push_back(1);
for (list<int>::iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
l.remove(1);
cout<<"删除所有等于2的元素: ";
for (list<int>::iterator it = l.begin(); it != l.end(); ++it)
{
cout << *it << " ";
}
cout << endl;
}
其它:
splice()对两个链表进行结合
unique()删除相邻重复元素
clear()删除所有元素
merge()合并两个链表并使之默认升序
模拟实现MyList
template<class T>
struct _ListNode
{
_ListNode<T>* _next;
_ListNode<T>* _prev;
T _data;
_ListNode(const T &x)
:_data(x)
,_next(NULL)
,_prev(NULL)
{}
};
//迭代器
template<class T,class Ref,class Ptr>
struct _ListIterator
{
typedef _ListIterator<T,Ref,Ptr>;
typedef _ListNode<T>Node;
Node* _node;
_ListIterator(Node* node)
:_node(node)
{}
Ref operator*()
{
return _node->_data;
}
Ptr operator->()
{
return &(_node->_data);
}
Self& operator++()//前置++
{
_node = _node->_next;
return *this;
}
Self operator++(int)//后置++
{
Self tmp(*this);
_node = _node->_next;
return tmp;
}
Self& operator--()//前置--
{
_node = _node->_prev;
return *this;
}
Self operator--(int)//后置--
{
Self tmp(*this);
_node = _node-_prev;
return tmp;
}
bool operator != (const Self& s)
{
return _node != s._node;
}
bool operator == (const Self& s)
{
return _node == s._node;
}
};
template <class T>
class List
{
typedef _ListNode<T> Node;
public:
typedef _ListIterator<T,T&,T*>Iterator;
typedef _ListIterator<T,const T&,const T*>ConstIterator;
Iterator Begin()
{
return Iterator(_head->_next);
}
ConstIterator Begin()const
{
return ConstIterator(_head->_next);
}
Iterator End()
{
return Iterator(_head);
}
ConstIterator End()const
{
return ConstIterator(_head);
}
List()
:_head(new Node(T()))
{
_head->_next = _head;
_head->_prev = _head;
}
void PushBack(const T& x)
{
Insert(End(),x);
}
void PopBack()
{
Erase(--End());
}
void PushFront(const T& x)
{
Insert(Begin(),x);
}
void PopFront()
{
Erase(Begin());
}
void Insert(Iterator pos,const T& x)
{
Node* cur = pos._node;
Node* prev = cur->_prev;
Node*tmp = new Node(x);
prev->_next = tmp;
tmp->_prev = prev;
tmp->_next = cur;
cur->_prev = tmp;
}
Iterator Earse(Iterator & pos)
{
assert(pos!=End());
Node* prev = (pos._node)->_prev;
Node* next = (pos._node)->_next;
prev->_next = next;
next->_prev = prev;
delete pos_node;
return next;
}
void PrintList()
{
Node* cur = Begin()._node;
while (cur != End()._node)
{
cout << cur->_data << " ";
cur = cur->_next;
}
cout << endl;
}
protected:
Node* _head;
}