c++中常用的容器【STL】

vector  变长数组 ,倍增思想

string 字符串 substr(), c_str()

queue  push(), front(), pop()

priority_queue 优先队列 push(), top(), pop()

stack 栈 push(), top(), pop()

deque 双端队列,队头队尾都可插入删除、且支持随机访问

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

unordered_set, unordered_map, unordered_multiset, unordered_multimap 哈希表

bitset 压位

vector容器

vector的各种操作:

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector> 

using namespace std;

int main()
{
    vector<int> a(3, 5);   定义,十个3
    vector<int> a[10];
    a.size();
    a.empty();  返回值是true 或 false
    a.clear();  清空操作,并非所有容器都有
    这两个函数对于所有容器都可实现,时间复杂度是为o(1)的
    系统为某一程序分配空间时, 所需时间,与空间大小无关, 与申请次数有关
    front() / back()
    push_back()/ pop_back();
    begin()/ end() 
    for(auto x : a) cout<<x<<endl;
    
    三种遍历方式
    vector<int> a;
    for(int i = 0; i < 10; i++) a.push_back(i);
    for(int i = 0; i < 10; i++) cout<<a[i]<<' ';
    cout<<endl;
    
    利用迭代器进行访问
    vector<int>::iterator 可利用auto进行替换
    for(/*vector<int>::iterator*/ auto i = a.begin(); i != a.end(); i++) cout<<*i<<endl;
    cout<<endl;
    
    for(auto x : a) cout<<x<<' ';
    cout<<endl;
    

    vector还支持比较大小
    vector<int> b(4,3), c(3,4);
    
    if(b < c) puts("b < c");
    



    return 0;
}

pair二元组

pair也支持比较, 按字典序
pair<int, string> p
p.first      二元组中的第一个元素
p.second     二元组中的第二个元素

string容器

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector> 

using namespace std;

常规操作:
size()
empty()
clear()


int main()
{
    string a = "yxc";
    
    a+= "de";
    a += 'x';
    
    cout<<a<<endl;
    
    a = yxcdex
 
    cout<< a.substr(1, 2) <<endl;
    第一个参数为字符串的起始地址【首元素为1的】, 第二个参数为数组长度
    a = xc

    printf("%s\n", a.c_str());
    a = yxcdex;     输出整个字符串
    
    
    
    return 0;
}

queue容器

/*
vector  变长数组 ,倍增思想
size()
empty()
clear()
front()/ back()
push_back()/ pop_back()
begin()/end()
[]
支持比较运算 按字典序

pair<int, int>
  first 第一个元素
  second 第二个元素
  支持比较运算, 以first为第一关键字, second为第二关键字(字典序)

string 字符串 
size();
empty()
clear()
substr(), c_str()

queue, 队列
push() 向队尾插入一个元素
front() 返回队头元素
back()  返回队尾元素
pop()   弹出队头元素

priority_queue 优先队列 push(), top(), pop()

stack 栈 push(), top(), pop()

deque 双端队列,队头队尾都可插入删除、且支持随机访问

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

unordered_set, unordered_map, unordered_multiset, unordered_multimap 哈希表

bitset 压位
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector> 
#include <queue>

using namespace std;

int main()
{
    queue<int> q;
    
    //q.clear();   queue没有清空这个函数
    
    q = queue<int>();   //清空操作
    
    return 0;
}

priority_queue优先队列

/*
vector  变长数组 ,倍增思想
size()
empty()
clear()
front()/ back()
push_back()/ pop_back()
begin()/end()
[]
支持比较运算 按字典序

pair<int, int>
  first 第一个元素
  second 第二个元素
  支持比较运算, 以first为第一关键字, second为第二关键字(字典序)

string 字符串 
size();
empty()
clear()
substr(), c_str()

queue, 队列
push() 向队尾插入一个元素
front() 返回队头元素
back()  返回队尾元素
pop()   弹出队头元素
 
priority_queue 优先队列   默认是大根堆
push()  插入一个元素
top()   返回堆顶元素
pop()   弹出堆顶元素

stack 栈 push(), top(), pop()

deque 双端队列,队头队尾都可插入删除、且支持随机访问

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

unordered_set, unordered_map, unordered_multiset, unordered_multimap 哈希表

bitset 压位
*/
#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector> 
#include <queue>

using namespace std;

int main()
{
    priority_queue<int> heap;
    
    priority_queue<int, vector<int>, greater<int>> heap;   小根堆
    return 0;
}

其他...未完待续【注释】


vector  变长数组 ,倍增思想
size()
empty()
clear()
front()/ back()
push_back()/ pop_back()
begin()/end()
[]
支持比较运算 按字典序

pair<int, int>
  first 第一个元素
  second 第二个元素
  支持比较运算, 以first为第一关键字, second为第二关键字(字典序)

string 字符串 
size() / length() 返回长度
empty()
clear()
substr(), c_str()

queue, 队列
push() 向队尾插入一个元素
front() 返回队头元素
back()  返回队尾元素
pop()   弹出队头元素
 
priority_queue 优先队列   默认是大根堆
push()  插入一个元素
top()   返回堆顶元素
pop()   弹出堆顶元素

stack 栈 
push() 栈顶插入元素
top()  返回栈顶元素
pop()  弹出堆顶元素

deque 双端队列,队头队尾都可插入删除、且支持随机访问
加强版的vector
size()
empty()
clear()
front()
back()
push_back() / pop_back()
push_front() / pop_front()
begin() / end()
[]

set, map, multiset, multimap   基于平衡二叉树(红黑树)实现,动态维护有序序列
    size()
    empty()
    clear()
    
    set / multiset
         insert()    插入一个数
         find()      查找一个数
         count()     返回某一个数的个数
         erase()
              (1)输入是一个数x,删除所有x
              (2)输入是一个迭代器, 删除这个迭代器
         lower_bound() /upper_bound()
           lower_bound(x)  返回大于等于x的最小的数
           upper_bound(x)  返回大于x的最小的数
    map / multimap
         insert()  插入的数是一个pair
         erase()   输入的参数是pair或者迭代器
         find()
         []

unordered_set, unordered_map, unordered_multiset, unordered_multimap 哈希表
    和上面的类似 增删改查的时间复杂度是o(1)的
    不支持lower_bound() / upper_bound()

bitset 压位

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#include<vector> 
#include <queue>
#include <map>

using namespace std;

int main()
{
    map<string, int> a;
    a["yxc"] = 1;
    
    cout<< a["yxc"] <<endl;
    
    
    
    return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值