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(otherLikeContainer) get_allocator()

本文详细介绍了C++中的容器,包括序列容器如vector、deque、list,容器适配器如stack、queue、priority_queue,关联容器如map、multimap、set、multiset,以及特殊容器string的功能和用法。重点梳理了各个容器的共有和特有函数,是学习C++容器的实用参考。
最低0.47元/天 解锁文章
3154

被折叠的 条评论
为什么被折叠?



