C++中的容器
容器与容器适配器
容器包括vector, deque, list, map, multimap, set, multiset。容器适配器包括基于deque的stack和queue,基于vector的priority_queue。string也实现了stl的接口。
因为编写C++程序时经常需要查找容器的函数接口,故作此总结。C++新引入的容器与函数未引入。主要参考自:STL Containers and Container Adaptors
序列容器
包括vector,deque,list
共有函数
- 构造函数
ContainerType<T> c; ContainerType<T> c(num); ContainerType<T> c(num, val); ContainerType<T> c(inIterBegin, inIterEnd); //复制构造函数 ContainerType<T> c(otherLikeContainer); ContainerType<T> c = otherLikeContainer;
- 赋值构造函数
c1 = c2
- 比较运算
c1 == c2 c1 != c2 c1 < c2 //按元素逐个比较 c1 <= c2 c1 > c2 c1 >= c2
- 容量
empty() size() max_size() resize(num, val = default)
- 迭代器和引用
begin() end() rbegin() rend() front() back()
- 插入值
push_back(val) insert(iter, val) insert(iter, num, val) insert(iter, inIterBegin, inIterEnd)
- 赋值(换掉容器内所有元素)
assign(inIterBegin, inIterEnd) assign(num, val)
- 删除元素
pop_back() erase(iter) erase(iterBegin, iterEnd) clear()
- 其他
swap(oth