C++中的容器

本文详细介绍了C++中的容器,包括序列容器如vector、deque、list,容器适配器如stack、queue、priority_queue,关联容器如map、multimap、set、multiset,以及特殊容器string的功能和用法。重点梳理了各个容器的共有和特有函数,是学习C++容器的实用参考。
摘要由CSDN通过智能技术生成

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
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值