C++学习记录之deque容器

本文详细介绍了deque容器的基本用法,包括迭代器、反向迭代器、赋值、索引运算、容器属性检查、插入与删除元素、在末尾与开头插入元素、调整容器大小、元素交换等核心功能。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

deque容器详解

1、c.begin()返回指向第一个元素的迭代器;c.end()返回指向最后一个元素下一个位置的迭代器

deque<int> d {1,2,3,4,5}; 
deque<int>::iterator it; 
for(it=d.begin();it!=d.end();it++)
{ 
  cout << *it << " ";      
} 
cout << endl;

2、c.rbegin()返回指向反向队列的第一个元素的迭代器(即原队列的最后一个元素);c.rend()返回指向反向队列的最后一个元素的下一个位置(即原队列的第一个元素的前一个位置)

deque<int> d {1,2,3,4,5}; 
deque<int>::reverse_iterator it; 
for(it=d.rbegin();it!=d.rend();it++)
{ 
  cout << *it << " ";     
} 
cout << endl;

3、c.assign(n,num)将n个num拷贝复制到容器c;c.assign(beg,end)将[beg,end)区间的数据拷贝复制到容器c

deque<int> d1 {1,2,3,4,5},d2;     
d2.assign(2, 8);     
deque<int>::iterator it;     
cout << "d2.assign(n,num):";     
for(it=d2.begin();it!=d2.end();it++)
{         
  cout << *it << " ";     
}     
d2.assign(d1.begin(), d1.begin()+3);    
cout << "d2.assign(beg,end):";     
for(it=d2.begin();it!=d2.end();it++)
{         
  cout << *it << " ";     
}     
cout << endl;

4、c.at(pos)返回索引为pos的位置的元素,会执行边界检查,如果越界抛出out_of_range异常

deque<int> d {1,2,3,4,5};     
cout << "d.at(pos):" << d.at(2); 

5、c.operator[]下标运算符重载

deque<int> d {1,2,3,4,5};    
cout << "d[2]:" << d[2];

6、c.empty()判断c容器是否为空

7、c.front()返回c容器的第一个元素;c.back()返回c容器的最后一个元素

8、c.size()返回c容器中实际拥有的元素个数

9、c.clear()清除c容器中拥有的所有元素

10、c.insert(pos,num)在pos位置插入元素num;c.insert(pos,n,num)在pos位置插入n个元素numc.insert(pos,beg,end)在pos位置插入区间为[beg,end)的元素

deque<int> d {1,2,3,4,5};
deque<int>::iterator it;
cout << "insert before:" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;
d.insert(d.end(),22);
d.insert(d.end(), 3,88);
int a[5] = {1,2,3,4,5};
d.insert(d.begin(),a,a+3);
cout << "insert after:" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;

11、c.erase(pos)删除pos位置的元素c.erase(beg,end)删除区间为[beg,end)的元素

deque<int> d {1,2,3,4,5};
d.erase(d.begin());
deque<int>::iterator it;
cout << "erase(pos) after:" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;
d.erase(d.begin(), d.begin()+3);
cout << "erase(beg,end) after:" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;

12、c.push_back(num)在末尾位置插入元素c.pop_back()删除末尾位置的元素c.push_front(num)在开头位置插入元素c.pop_front()删除开头位置的元素

deque<int> d {1,2,3,4,5};
d.push_back(10);
deque<int>::iterator it;
cout << "push_back(num):" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;
    
d.pop_back();
cout << "pop_back(num):" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;
    
d.push_front(10);
cout << "push_front(num):" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;
    
d.pop_front();
cout << "pop_front(num):" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;

13、c.resize(num)从新定义容器的大小

deque<int> d {1,2,3,4,5};
cout << "d.size():" << d.size() << endl;
d.resize(d.size()+5);
cout << "d.resize() after:" << d.size() <<endl;
deque>int<::iterator it;
cout << "resize() after:" ;
for(it=d.begin();it!=d.end();it++){
  cout << *it << " ";
}
cout << endl;

14、c1.swap(c2)交换容器c1,c2;swap(c1,c2)同上。

deque<int> d1 {1,2,3,4,5},d2,d3;
d1.swap(d2);
deque<int>::iterator it;
cout << "d1 swap after:" ;
for(it=d1.begin();it!=d1.end();it++){
  cout << *it << " ";
}
cout << endl;
cout << "d2 swap after:" ;
for(it=d2.begin();it!=d2.end();it++){
  cout << *it << " ";
}
cout << endl;
    
swap(d3,d2);
cout << "d3 swap after:" ;
for(it=d3.begin();it!=d3.end();it++){
  cout << *it << " ";
}
cout << endl;
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值