C++学习笔记总结练习:容器适配器

容器适配器

目录

0 简介

概念

  • 适配器 (adaptor) 是标准库的一个通用概念。容器、类和函数都有适配器。 本质上, 一个适配器是一种机制, 能使某种事物的行为看起来像另外一种事物一样。。一个容器适配器接受一种己有的容器类型, 使其行为看起来像一利1不同的类型。
  • 添加额外操作,实现某种特殊的数据结构。

容器适配器的操作

  • 可以用顺序容器初始化适配器。使用的是顺序容器的拷贝。

1 stack

  • 默认基于deque实现,也可以基于list/vector

概念

特有操作

2 queue

概念

  • queue是基于deque实现的,也可以用vector或list
  • priority_queue是基于vector实现的。也可以用deque实现

特有操作

3 priority_queue

初始化

  • Type 就是数据类型,Container 就是容器类型(Container必须是用数组实现的容器,比如vector,deque等等,但不能用 list。STL里面默认用的是vector),Functional 就是比较的方式,当需要用自定义的数据类型时才需要传入这三个参数,使用基本数据类型时,只需要传入数据类型,默认是大顶堆

    //构造函数
    priority_queue<Type, Container, Functional>

    //升序队列
    priority_queue <int,vector<int>,greater<int> > q;
    //降序队列
    priority_queue <int,vector<int>,less<int> >q;

基本操作

和队列基本操作相同:

  • top 访问队头元素
  • empty 队列是否为空
  • size 返回队列内元素个数
  • push 插入元素到队尾 (并排序)
  • emplace 原地构造一个元素并插入队列
  • pop 弹出队头元素
  • swap 交换内容
C++ 复制代码
#include<iostream>
#include <queue>
using namespace std;
int main() 
{
    //对于基础类型 默认是大顶堆
    priority_queue<int> a; 
    //等同于 priority_queue<int, vector<int>, less<int> > a;
    
    //             这里一定要有空格,不然成了右移运算符↓
    priority_queue<int, vector<int>, greater<int> > c;  //这样就是小顶堆
    priority_queue<string> b;

    for (int i = 0; i < 5; i++) 
    {
        a.push(i);
        c.push(i);
    }
    while (!a.empty()) 
    {
        cout << a.top() << ' ';
        a.pop();
    } 
    cout << endl;

    while (!c.empty()) 
    {
        cout << c.top() << ' ';
        c.pop();
    }
    cout << endl;

    b.push("abc");
    b.push("abcd");
    b.push("cbd");
    while (!b.empty()) 
    {
        cout << b.top() << ' ';
        b.pop();
    } 
    cout << endl;
    return 0;
}

pair优先队列

  • 默认的比较顺序是:pari的比较,先比较第一个元素,第一个相等比较第二个
C++ 复制代码
#include <iostream>
#include <queue>
#include <vector>
using namespace std;
int main() 
{
    priority_queue<pair<int, int> > a;
    pair<int, int> b(1, 2);
    pair<int, int> c(1, 3);
    pair<int, int> d(2, 5);
    a.push(d);
    a.push(c);
    a.push(b);
    while (!a.empty()) 
    {
        cout << a.top().first << ' ' << a.top().second << '\n';
        a.pop();
    }
}

自定义类型的优先队列

  • 自定义重载比较运算符><的类和结构体。
  • 自定义重载函数调用运算符()的类或者结构体。需要接受两个参数
  • 或者使用自定义的函数指针。需要接受两个参数
  • 或者使用lambda比较函数。需要接受两个参数。
C++ 复制代码
#include <iostream>
#include <queue>
using namespace std;

//方法1
struct tmp1 //运算符重载<
{
    int x;
    tmp1(int a) {x = a;}
    bool operator<(const tmp1& a) const
    {
        return x < a.x; //大顶堆
    }
};

//方法2
struct tmp2 //重写仿函数
{
    bool operator() (tmp1 a, tmp1 b) 
    {
        return a.x < b.x; //大顶堆
    }
};

int main() 
{
    tmp1 a(1);
    tmp1 b(2);
    tmp1 c(3);
    priority_queue<tmp1> d;
    d.push(b);
    d.push(c);
    d.push(a);
    while (!d.empty()) 
    {
        cout << d.top().x << '\n';
        d.pop();
    }
    cout << endl;

    priority_queue<tmp1, vector<tmp1>, tmp2> f;
    f.push(c);
    f.push(b);
    f.push(a);
    while (!f.empty()) 
    {
        cout << f.top().x << '\n';
        f.pop();
    }
}

4 bitset特殊容器

头文件

复制代码
#include<bitset>

定义和初始化

复制代码
bitset<32> bitvec(1U);
  • 编号从0开始的二进制位被称为低位。编号31结束的二进制位被称为高位。

bitset操作

  • bitset支持位运算
相关推荐
头发还没掉光光4 分钟前
Linux网络初始及网络通信基本原理
linux·运维·开发语言·网络·c++
lkbhua莱克瓦2427 分钟前
Java基础——常用算法4
java·数据结构·笔记·算法·github·排序算法·快速排序
m0_7482480240 分钟前
揭开 C++ vector 底层面纱:从三指针模型到手写完整实现
开发语言·c++·算法
海盗猫鸥40 分钟前
「C++」string类(2)常用接口
开发语言·c++
序属秋秋秋42 分钟前
《Linux系统编程之开发工具》【实战:倒计时 + 进度条】
linux·运维·服务器·c语言·c++·ubuntu·系统编程
学渣6765643 分钟前
11111
笔记
MeowKnight95844 分钟前
【DIY】PCB练习记录2——51单片机核心板
笔记
py有趣7 小时前
LeetCode算法学习之两数之和 II - 输入有序数组
学习·算法·leetcode
BreezeJuvenile7 小时前
外设模块学习(15)——MQ-2烟雾气体传感器(STM32)
stm32·单片机·学习·mq-2·烟雾气体传感器
tjsoft8 小时前
网站如何被百度收录之探索笔记
笔记