C++ list容器和deque容器的基本操作

deque容器

1.1 deque容器基本概念

功能:双端数组,可以对头端进行插入和删除操作
deque与vector区别:
*vector对于头部的插入删除效率低,数据量越大,效率越低。
*deque相对而言,对头部的插入删除速度比vector快
*vector访问元素时的速度会比deque快,这和两者内部实现有关。 

函数原型:
deque<T>deqT;//默认构造形式
deque(beg,end);//构造函数将(beg,end)区间中的元素拷贝给本身
deque(n,elem);//构造函数将n个elem拷贝给本身
deque(const deque &deq);//拷贝构造函数

#include <iostream>
#include <deque>
using namespace std;
​
void printDeque01(deque<int> &d) {
    for (deque<int>::iterator it = d.begin(); it != d.end(); it++) {
        *it = 100;
        cout << *it << " ";
    }
    cout << endl;
}
​
//若想函数为只读属性,可加const
void printDeque02(const deque<int> &d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        //*it=100;//容器中的数据不可以修改了
        cout << *it << " ";
    }
    cout << endl;
}
//deque构造函数
void test01() {
    deque<int>d1;
    for (int i = 0; i < 10; i++) {
        d1.push_back(i);
    }
    printDeque02(d1);
    //区间赋值
    deque<int>d2(d1.begin(), d1.end());
    printDeque02(d2);
    //10个100
    deque<int>d3(10, 100);
    printDeque02(d3);
    //拷贝构造
    deque<int>d4(d3);
    printDeque02(d4);
    return;
}
int main() {
    test01();
    system("pause");
    return 0;
}

1.2 deque赋值操作

#include <iostream>
#include <deque>
using namespace std;
​
void printDeque(const deque<int> &d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    return;
}
​
//deque容器的赋值操作
void test01() {
    deque<int>d1;
    for (int i = 0; i < 10; i++) {
        d1.push_back(i);
    }
    printDeque(d1);
    //operator=赋值
    deque<int>d2;
    d2 = d1;
    printDeque(d2);
    //assign赋值
    deque<int>d3;
    d3.assign(d1.begin(), d1.end());
    printDeque(d3);
    //10个100
    deque<int>d4;
    d4.assign(10, 100);
    printDeque(d4);
    return;
}
​
int main() {
    test01();
    system("pause");
    return 0;
}

1.3 deque大小操作

#include <iostream>
#include <deque>
using namespace std;
​
void printDeque(deque<int> &d) {
    for (deque<int>::const_iterator it = d.begin(); it != d.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
//deque容器大小操作
void test01() {
    deque<int>d1;
    for (int i = 0; i < 10; i++) {
        d1.push_back(i);
    }
    if (d1.empty()) {
        cout << "d1为空" << endl;
    } else {
        cout << "d1不为空" << endl;
        cout << "d1的大小" << d1.size() << endl;
        //注deque容器没有容量概念
    }
    //重新指定大小
    deque<int>d2 = d1;
    d1.resize(15);
    printDeque(d1);
    d2.resize(15, 1);
    printDeque(d2);
​
    d1.resize(5);
    printDeque(d1);
    return;
}
​
int main() {
    test01();
    system("pause");
    return 0;
}
总结:
*deque没有容量概念
*判断是否为空--empty();
*返回元素个数--size();
*重新指定个数--resize();

1.4 deque插入和删除

函数原型:
两端插入操作:
push_back(elem);//在容器末尾添加一个数据
push_front(elem);//在容器头部插入一个数据
pop_back();//删除容器最后一个数据
pop_front();//删除容器第一个数据
指定位置操作:
insert(pos,elem);//在pos位置插入一个elem元素,返回新数据位置
insert(pos,n,elem);//在pos位置插入n个elem数据,无返回值
insert(pos,beg,end);//在pos位置插入[beg,end]区间数据,无返回值
clear();//清空容器的所有数据
erase(beg,end);//删除[beg,end]区间的数据,返回下一个数据的返回值
erase(pos);//删除pos位置的数据,返回下一个数据的位置

#include<iostream>
#include<deque>
using namespace std;
void printDeque(const deque<int>&d){
    for(deque<int>::const_iterator it=d.begin();it!=d.end();it++){
        cout<<*it<<" ";
    }   
    cout<<endl;
    return;
}
//deque容器的插入和删除
//两端操作:
void test01(){
    deque<int>d1;
    //尾插
    d1.push_back(10);
    d1.push_back(20);
    //头插
    d1.push_front(100);
    d1.push_front(200);
    
    printDeque(d1);//200 100 10 20
    
    //尾删
    d1.pop_back();
    printDeque(d1);
    //头删
    d1.pop_front();
    printDeque(d1);
    
    return;
}
void test02(){
    deque<int>d1;
    d1.push_back(10);
    d1.push_back(20);
    d1.push_front(100);
    d1.push_front(200);
    printDeque(d1);//200 100 10 20
    //insert插入
    d1.insert(d1.begin(),1000);
    printDeque(d1);
    d1.insert(d1.begin(),2,10000);//10000 10000 1000 200 100 10 20
    printDeque(d1);
    //按照区间进行插入
    deque<int>d2;
    d2.push_back(1);
    d2.push_back(2);
    d2.push_back(3);
    d1.insert(d1.begin(),d2.begin(),d2.end());
    printDeque(d1);
    //1 2 3 10000 10000 1000 200 100 10 20
    return;
}
void test03(){
    deque<int>d1;
    d1.push_back(10);
    d1.push_back(20);
    d1.push_front(100);
    d1.push_front(200);
    //删除
    deque<int>::iterator it=d1.begin();
    it++;
    d1.erase(it++);
    //200 10 20
    printDeque(d1);
    //按区间方式删除
    d1.erase(d1.begin(),d1.end());//相当于d1.clear();
    printDeque(d1);
    return;
}
int main(){
    test01();
    test02();
    test03();
    system("pause");
    return 0;
}
总结:
***插入和删除提供的位置时迭代器。
*尾插---push_back
*尾删---pop_back
*头插---push_front
*头删---pop_front

1.5 deque数据存取

函数原型:
at(int idx);//返回所以idx所指的数据
operator[];//返回索引idx所指的数据
front();//返回容器中第一个数据元素
back();//返回容器中最后一个数据元素

#include<iostream>
#include<deque>
using namespace std;
//deque容器数据存取操作
void test01(){
    deque<int>d;
    d.push_back(10);
    d.push_back(20);
    d.push_back(30);
    d.push_back(100);
    d.push_back(200);
    d.push_back(300);
    //通过[]方式访问元素
    //300 200 100 10 20 30
    for(int i=0;i<d.size();i++){
        cout<<d[i]<<" ";
    }   
    cout<<endl;
    //通过at方式访问
    for(int i=0;i<d.size();i++){
        cout<<d.at(i)<<" ";
    }
    cout<<endl;
    cout<<"第一个元素为:"<<d.front()<<endl;
    cout<<"最后一个元素为:"<<d.back()<<endl;
    return;
}
​
int main(){
    test01();
    system("pause");
    return 0;
}
总结:
*除了用迭代器获取deque容器中元素,[]和at也可以
*front返回容器第一个元素
*back返回容器最有一个元素

1.6 deque排序

功能:利用算法实现deque容器进行排序
算法:sort(iterator beg,iterator end);//对beg和end区间内元素进行排序

#include<iostream>
#include<algorithm>
#include<deque>
using namespace std;
void printDeque(const deque<int>&d){
    for(deque<int>::const_iterator it=d.begin();it!=d.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    return;
}
void test01(){
    deque<int>d;
    d.push_back(10);
    d.push_back(20);
    d.push_back(30);
    d.push_front(100);
    d.push_front(200);
    d.push_front(300);
    //300 200 100 10 20 30
    printDeque(d);
    sort(d.begin(),d.end());
    cout<<"排序后:"<<endl;
    printDeque(d);
    return;
}
int main(){
    test01();
    system("pause");
    return 0;
}

List 容器

1.1 list基本概念

优点:可以对任意位置进行快速插入和删除元素

缺点:容器的遍历速度没有数组快。占用空间比数组大。

由于链表的存储方式并不是连续的内存空间,因此链表list中的迭代器只支持前移和后移,属于双向迭代器。

总结:STL中list和vector是两个最常被使用的容器,各有各的缺点。

1.2 list构造函数

#include <iostream>
#include <list>
​
using namespace std;
​
void printList(const list<int> &L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
//list容器构造函数
void test01() {
    //创建list容器
    list<int>L1;
    //添加数据
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    //遍历容器:
    printList(L1);
    //区间方式构造:
    list<int>L2(L1.begin(), L1.end());
    printList(L2);
    //拷贝构造
    list<int>L3(L2);
    printList(L3);
    //n个elem
    list<int>L4(10, 1000);
    printList(L4);
    return;
}
​
int main() {
    test01();
    system("pause");
    return 0;
}

1.3 list赋值和交换

#include <iostream>
#include <list>
​
using namespace std;
​
//list容器赋值和交换
void printList(const list<int> &L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    return;
}
​
//list容器赋值和交换
void test01() {
    list<int>L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    printList(L1);
​
    list<int>L2;
    L2 = L1; //operator=赋值
    printList(L2);
​
    list<int>L3;
    L3.assign(L2.begin(), L2.end());
    printList(L3);
​
    list<int>L4;
    L4.assign(10, 100);
    printList(L4);
    return;
}
​
void test02() {
    list<int>L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    printList(L1);
​
    list<int>L2;
    L2.assign(10, 100);
    cout << "交换前:" << endl;
    printList(L1);
    printList(L2);
    L1.swap(L2);
    cout << "交换后:" << endl;
    printList(L1);
    printList(L2);
​
    return;
}
​
int main() {
    test01();
    test02();
    system("pause");
    return 0;
}
​

1.4 list大小操作

#include<iostream>
#include<list>
​
using namespace std;
void printList(const list<int>&L){
    for(list<int>::const_iterator it=L.begin();it!=L.end();it++){
        cout<<*it<<" ";
    }
    cout<<endl;
    return;
}
//list 容器大小操作
void test01(){
    list<int>L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    printList(L1);
    //判断容器是否为空
    if(L1.empty()){
        cout<<"L1不为空"<<endl;
    }
    else{
        cout<<"L1不为空"<<endl;
        cout<<"L1的元素个数:"<<L1.size()<<endl;
    }
    //重新指定大小
    L1.resize(10,10000);
    printList(L1);//10 20 30 40 10000 10000 10000 10000 10000 10000
     
    L1.resize(2);
    printList(L1);
    
    return;
}
int main(){
    test01();
    system("pause");
    return 0;
}

1.5 list容器的插入和删除

#include <iostream>
#include <list>
​
using namespace std;
​
void printList(const list<int> &L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
    return;
}
​
//list容器的插入和删除
void test01() {
    list<int>L;
    //尾插
    L.push_back(10);
    L.push_back(20);
    L.push_back(30);
    //头插
    L.push_front(100);
    L.push_front(200);
    L.push_front(300);
    printList(L);//300 200 100 10 20 30
    //尾删pop
    L.pop_back();
    printList(L);
    //头删
    L.pop_front();
    printList(L);
    //insert插入
    list<int>::iterator it = L.begin();
    L.insert(++it, 1000);
    L.insert(L.begin(), 1000);
    printList(L);//200 1000 100 10 20
    //删除
    it = L.begin();
    L.erase(++it);
    //200 100 10 20
    printList(L);
    //移除
    L.push_back(10000);
    L.push_back(10000);
    L.push_back(10000);
    L.push_back(10000);
    L.push_back(10000);
    printList(L);
    L.remove(10000);//将所有的10000全部删掉
    printList(L);
    //清空
    L.clear();
    printList(L);
    return;
}
​
int main() {
    test01();
    system("pause");
    return 0;
}

1.6 list数据存取

#include <iostream>
#include <list>
​
using namespace std;
​
void test01() {
    list<int>L1;
    L1.push_back(10);
    L1.push_back(20);
    L1.push_back(30);
    L1.push_back(40);
    //L1[0]不可以用[]方式访问list容器中的元素
    //L1.at(0)不可以用at方式访问list容器中的元素
    //原因是list本质是链表,不是用连续线性空间存储数据,迭代器也是不支持随机访问的
    cout << "第一个元素为:" << L1.front() << endl;
    cout << "最后一个元素为:" << L1.back() << endl;
    //验证迭代器是不支持随机访问的
    list<int>::iterator it = L1.begin();
    it++;//支持双向
​
    //it=it+1//不支持随机访问
    return;
}
​
int main() {
    test01();
    system("pause");
    return 0;
}
总结:
*list容器不可以通过[]或at方式访问数据
*返回第一个元素list--front
*返回最有一个元素--back

1.7 list反转和排序

功能描述:
将容器中的元素反转,以及将容器中的数据进行排序。
函数原型:
reverse();//反转链表
sort();//链表排序

#include <iostream>
#include <list>
#include <algorithm>
​
using namespace std;
​
void printList(const list<int> &L) {
    for (list<int>::const_iterator it = L.begin(); it != L.end(); it++) {
        cout << *it << " ";
    }
    cout << endl;
}
​
//list容器反转和排序
void test01() {
    list<int>L1;
    L1.push_back(20);
    L1.push_back(10);
    L1.push_back(40);
    L1.push_back(50);
    L1.push_back(30);
    cout << "反转前:" << endl;
    printList(L1);
    //反转
    cout << "反转后:" << endl;
    L1.reverse();
    printList(L1);
    return;
}
​
bool myCompare(int v1, int v2) {
    //降序 就让第一个数>第二个数
    return v1 > v2;
}
​
void test02() {
    list<int>L1;
    L1.push_back(20);
    L1.push_back(10);
    L1.push_back(40);
    L1.push_back(50);
    L1.push_back(30);
    cout << "排序前:" << endl;
    printList(L1);
    //排序
    //所有不支持随机访问迭代器的容器,不可以用标准算法
    //不支持随机访问迭代器的容器,内部会提供一些算法
    //sort(L1.begin(), L1.end());
    L1.sort();//默认规则,从小到大,升序
    cout << "排序后:" << endl;
    printList(L1);
    L1.sort(myCompare);
    printList(L1);
    return;
}
​
int main() {
    test01();
    test02();
    system("pause");
    return 0;
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值