c++极简总结 — STL deque

本文详细介绍了STL中的deque容器,包括其与vector的区别、内部实现原理、构造函数、赋值操作、容器大小及元素存取、插入删除等操作。deque作为双端队列,其头尾插入速度优于vector,并通过内部的中控器管理多个缓冲区。示例代码展示了deque的基本用法,如构造、赋值、大小调整和元素访问。
摘要由CSDN通过智能技术生成

STL deque

deque

deque (usually pronounced like "deck") is an irregular acronym of double-ended queue
双端数组,可以对头端和尾端进行插入删除操作。

1、deque与vector的头插速度
  • vector 对于头部的插入效率低,因为需要向后移动所有元素。
  • deque 相对vector 要快。
2 、deque 内部中控器

deque 内部有个中控器,维护每段缓冲区中的内容,缓冲区中存放真实的数据,中控器维护每个缓冲区的地址。

3、相关操作
3.1 构造函数

类似于vector.

  • default constructor
  • fill constructor
explicit deque (size_type n);
         deque (size_type n, const value_type& val,
                const allocator_type& alloc = allocator_type())
  • range constructor
template <class InputIterator>
  deque (InputIterator first, InputIterator last,
         const allocator_type& alloc = allocator_type());
  • copy constructor (and copying with allocator)
deque (const deque& x);
deque (const deque& x, const allocator_type& alloc);
  • move constructor (and moving with allocator)
deque (deque&& x);
deque (deque&& x, const allocator_type& alloc);
  • initializer list constructor
deque (initializer_list<value_type> il,
       const allocator_type& alloc = allocator_type());

测试:

void test_1()
{
    std::deque<int> d1;          // 默认构造函数
    std::deque<int> d2 (4,100);  //four ints with value 100
    std::deque<int> d3 (d2.begin(),d2.end());  // iterating through second
    std::deque<int> d4 (d3);                       // a copy of third
    
    int myints[] = {16,2,77,29};
    std::deque<int> d5 (myints, myints + sizeof(myints) / sizeof(int) );

    std::cout << "The contents of d5 are:";
    for (std::deque<int>::iterator it = d5.begin(); it!=d5.end(); ++it)
        std::cout << ' ' << *it;
    std::cout << '\n';
}
3.2 赋值操作

与vector一样

void printDeque(const deque<int> &d)
{
    for(deque<int>::const_iterator it=d.begin();it!=d.end();it++)
        cout<<*it<<" ";
    cout<<endl;
}
void test_2()
{
    deque<int> d1 = {1,2,3,4,5,6,7,8};
    deque<int> d2;
    d2 = d1;
    printDeque(d1);
    printDeque(d2);

    deque<int> d3;
    d3.assign(++d2.begin(),--d2.end());
    printDeque(d3);

    deque<int> d4;
    d4.assign(5,10);  //5 个 10
    printDeque(d4);
}
3.3 容器大小,元素存取

empty() 判空
size() 元素个数
resize(int num) 重新指定容器大小
resize(int num,elem) 重新指定容器大小和填充的元素
at(int idx)
operator[]
front() 返回第一个
back 返回最后一个
测试

void test_3()
{
    deque<int> d1;
    cout<<d1.empty()<<endl;
    d1.push_back(1);
    d1.push_back(3);
    d1.push_back(4);
    d1.push_back(5);
    d1.push_back(8);
    cout<<d1.empty()<<endl;
    cout<<"d1.size() is "<<d1.size()<<endl;
    d1.resize(10);
    printDeque(d1);
    d1.resize(15,1);
    printDeque(d1);

    cout<<"d1.at(0) = "<<d1.at(0)<<endl;
    cout<<"d1[0] = "<<d1[0]<<endl;
    cout<<"d1 front:"<<d1.front()<<endl;
    cout<<"d1 back:"<<d1.back()<<endl;
}
3.4 插入删除

push_back(elem)
push_front(elm)
pop_back()
pop_front()
insert(pos,elem) 在位置pos ,插入elem
insert(pos,n,elem) 在位置pos ,插入n 个 elem
insert(pos,begin,end) 在位置pos ,插入n 个 elem
erase(begin,end)
erase(pos)
clear()
pos 均为迭代器指向的位置
测试

void test_4()
{
    deque<int> d1;
    d1.push_back(1);
    printDeque(d1);
    d1.push_front(2);
    printDeque(d1);
    d1.push_back(3);
    d1.push_back(5);
    printDeque(d1);
    d1.pop_front();
    d1.pop_back();
    printDeque(d1);

    d1.insert(++d1.begin(),5,20);
    printDeque(d1);
    d1.erase(d1.end());
    printDeque(d1);
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

FlyDremever

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值