> 文章列表 > 【STL】一文拿下所有C++stl

【STL】一文拿下所有C++stl

【STL】一文拿下所有C++stl

目录

1.vector, 变长数组,倍增的思想

2.pair<int,int>

3.string,字符串

4.queue, 队列

5.priority_queue, 优先队列,默认是大根堆

6.stack, 栈

7.deque, 双端队列

8.set, map, multiset, multimap, 基于平衡二叉树(红黑树),动态维护有序序列

    set/multiset

    map/multimap

    unordered_set, unordered_map, unordered_multiset, unordered_multimap, 哈希表

9.bitset, 圧位


1.vector, 变长数组,倍增的思想

  1.  size()  返回元素个数
  2.  empty()  返回是否为空
  3. clear()  清空
  4. front()/back()
  5.push_back()/pop_back()
  6. begin()/end()
  7. []
  8.支持比较运算,按字典序

#include <vector>//初始化
vector <int> a;
vector <int> a(10);      //长度为10的vector
vector <int> a(10,3);   //10个数,每个都为3
vector <int> a[10];      //定义10个vector数组//
a.size();//返回个数
a.empty();//判断是否空
a.clear();//清空//
a.front();//返回第一个数
a.back();//返回最后一个数//插入,删除
a.push_back();//向最后插入一个数
a.pop_back();//把最后一个数删掉
//例子
for(int i=0;i<10;i++)a.push_back(i);//迭代器
a.begin()//vector第0个数
a.end()//vector最后一个数的后边一个数    //遍历
//法一:正常数组遍历
//法二:迭代器for(auto i=a.begin();i!=a.end();i++) cout<<*i<<' ';  //vector<int>::iterator可写为auto
//法三: 范围遍历for(auto x :a)cout<<x<<' ';//支持比较运算
vector<int>a(3,4),b(4,3);
if(a<b)puts("YES");

2.pair<int, int>

当某个东西有两个不同的属性(三个)可以用pair存储,若排序则把要排序的属性放first,不排序 的关键词放second
   1. first, 第一个元素
   2.second, 第二个元素
   3.支持比较运算,以first为第一关键字,以second为第二关键字(字典序)

#include <vector>int main(){pair <int ,string>p;//取第一,二个元素
p.first;
p.second;//取第二个元素//初始化
p=make_pair(10,"wzf");//法一
p=(10,"wzf");         //法二//三个属性时
pair<int,pair<int,int>>p;}

3.string,字符串

   1. size()/length()  返回字符串长度
   2. empty()
   3. clear()
   4. substr(起始下标,(子串长度))  返回子串
   5. c_str()  返回字符串所在字符数组的起始地址

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <vector>using namespace std;
int main(){//添加操作
string a="wzf";
a+="fzw";
a+='a';//从第一个字符开始,输出长度为的10字符
cout<<a.substr(0,10)<<endl;cout<<a.substr(2)<<endl;//省略第二个参数,此例子则输出从第三个字符全部串//输出
cout<<a<<endl;//法一
printf("%s\\n",a.c_str());//法二,返回起始地址}

4.queue, 队列

先进先出

   1. size()
   2. empty()
   3. push()  向队尾插入一个元素
   4. front()  返回队头元素
   5. back()  返回队尾元素
   6. pop()  弹出队头元素

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>using namespace std;
int main(){//初始化
queue<int>q;//清空
q=queue<int>();}

5.priority_queue, 优先队列,默认是大根堆

    1.size()
    2.empty()
    3.push()  插入一个元素
    4. top()  返回堆顶元素
    5.pop()  弹出堆顶元素
    6.定义成小根堆的方式:priority_queue<int, vector<int>, greater<int>> q;

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
#include <queue>using namespace std;
int main(){//初始化
priority_queue<int> heap;//默认大根堆,改为小根堆
heap.push(-x);//法一:直接插入负数
priority_queue<int, vector<int>, greater<int>> q;//法二:定义时改变}

6.stack, 栈

    size()
    empty()
    push()  向栈顶插入一个元素
    top()  返回栈顶元素
    pop()  弹出栈顶元素

7.deque, 双端队列

    size()
    empty()
    clear()
    front()/back()
    push_back()/pop_back()
    push_front()/pop_front()
    begin()/end()
    []

8.set, map, multiset, multimap, 基于平衡二叉树(红黑树),动态维护有序序列

    size()
    empty()
    clear()
    begin()/end()
    ++, -- 返回前驱和后继,时间复杂度 O(logn)

    set/multiset

        insert()  插入一个数
        find()  查找一个数
        count()  返回某一个数的个数
        erase()
            (1) 输入是一个数x,删除所有x   O(k + logn)
            (2) 输入一个迭代器,删除这个迭代器
        lower_bound()/upper_bound()
            lower_bound(x)  返回大于等于x的最小的数的迭代器
            upper_bound(x)  返回大于x的最小的数的迭代器

    map/multimap

        insert()  插入的数是一个pair
        erase()  输入的参数是pair或者迭代器
        find()
        []  注意multimap不支持此操作。 时间复杂度是 O(logn)
        lower_bound()/upper_bound()

    unordered_set, unordered_map, unordered_multiset, unordered_multimap, 哈希表

    和上面类似,增删改查的时间复杂度是 O(1)
    不支持 lower_bound()/upper_bound(), 迭代器的++,--

9.bitset, 圧位

    bitset<10000> s;
    ~, &, |, ^
    >>, <<
    ==, !=
    []

    count()  返回有多少个1

    any()  判断是否至少有一个1
    none()  判断是否全为0

    set()  把所有位置成1
    set(k, v)  将第k位变成v
    reset()  把所有位变成0
    flip()  等价于~
    flip(k) 把第k位取反

//课程剩余1.46.15

哈雷车友社区