STL常用的容器

STL常用容器



常用STL有 (需要List 做算法题可以自己用数组实现,速度快,不用使用STL)

  • vector 变长数组 ,倍增思想

  • string 字符串 substr() 返回子串 ,c_str() 对应数组头指针

  • queue 队头插入push(), 返回队头front() ,pop()

  • priority_queue 优先队列 push() top() pop() 实际上是一个堆

  • stack 栈 push() top() pop()

  • deque 双端队列

  • set, map, muliset, multimap 基于平衡树(红黑树) 动态维护有序序列

  • unordered_set,unordered_map unordered_multiset unordered_multimap 哈希表

  • bitset 压位

#include<iostream>
#include<cstring>
#include<algorithm>
#include<cstdio>
using namespace std;

vector容器

#include<vector>
vector<int> b(10,-3);//长度为10 值全为-3的vector
vector<int> a;
int value=520;
a.push_back(value);//尾插
a.front();//返回第一个数
a.back();//返回最后一个数
a.size();//返回大小
a.empty();//是否为空
a.clear();//清空  不是删除
//迭代器
//返回迭代器
begin(),end();  
for(int i=0;i<10;i++) a.push_back(i);
//三种迭代方式
for(int i=0;i<a.size();i++)
{
	cout<<a[i]<<' ';
}
//可以把vector<int>::iterator替换成auto
for(vector<int>::iterator i =a.begin();i!=a.end();i++) //相当于指针
{
    cout<<*i<<' ';
}

for(auto x:a) cout<<x<<' ';
//支持比较运算
//字典序比较
vector<int> a(4,3),b(3,4);
if(a<b) puts("a<b");
//打印a<b

pair c++内置二元组 不需要头文件

pair<int,string> p;
p.first //第一个元素
p.second //第二个元素
p = make_pair(10,"abc")
p ={20,"abc"}; 
//vector<pair<int,string> > a;//>>不要连续写 以为是插入
//sort(a);//排序默认 first关键字
//储存三个元素
pair<int,pair<int ,string> > a;

string

#include<string>
string a;
a.size();
a.empty();
a.clear();
a= "abc";
a+="c";//--> a="abcc"
cout<<a.substr(1,2);//bc (下标,长度)
//用printf()打印
printf("%s",a.c_str());

queue 队列

#include<queue>
queue<int> a;
a.push();
a.empty();
a.front();
a.back();
a.pop();
a.size();
//注意没有a.clear()函数
//想要清空怎么办?
a = queue<int>();//就行了

priority_queue 优先队列

#include<queue>
priority_queue<int> heap;//默认大根堆
//如何定义小根堆? 也可以用x的相反数插入
#include<vector>
priority_queue<int,vector<int> ,greater<int> > heap;


stack 栈

#include<stack>
stack<int> a;
a.push();
a.size();
a.top();
a.pop();

deque 双端队列 速度异常的慢

#include<deque>
deque<int> a;
a.size();
a.empty();
a.clear();
a.front();a.back();
a.push_back();a.pop_back();
a.push_fron();a.pop_front();
a.begin();a.end();
//也可以用 a[i]访问

set map multiset multimap

//都支持的函数
size();
empty();
clear();//都支持
begin();
end();


#include<set>
set<int> s;
//set/multiset(后者元素可重复)
int value= 1;
s.insert(value); //都是O(logn)
s.find(value);
s.count(value);//计算某个数的个数 set只有01两种情况 multiset有多种
s.erase(value);//删除一个数  o(logn+k)
s.erase(iterator);//删除迭代器 
s.lower_bound(value);//返回>=value的最小的数的迭代器 不存在返回end()
s.upper_bound(value);//返回>value的最小的数

//map multimap
#include<map>
map<int,int> m;//储存映射关系
m.insert({1,2});
m.erase({1,2});
m.find({1,2});
m[1]==2;//可以当数组用 但是O(logn)
m.lower_bound(),m.upper_bound();

unordered_set,unordered_map unordered_multiset unordered_multimap

//和上面类似 时间复杂度为O(1)
//但是不支持lower_bound() 和upper_bound() 不支持迭代器++ --
#include<unordered_map>
undered_map<string,int> a={{"yt",1},{"wjx",2},{"lxw",3}};
printf("%d\n",a["yt"]);//  1

bitset 为了省内存 省8倍空间

//比如你想要 10000*10000的bool矩阵   10^8 B
//此时100MB了
#include<bitset>
bitset<10000> s;
//支持 ~,&,|,^,>>,<<,==,!=,[]
s.count();//返回有多少个1
s.any();//判断是否至少有一个1
s.none();//判断是否全为0
s.set();//所有位置变成1
s.set(k,v);//把k位变为0
s.reset();//所有置为0
s.flip();//等价与~
s.flip(k);//第k位取~

   bitset<2> bitset1(12);  //12的二进制为1100(长度为4),但bitset1的size=2,只取后面部分,即00

    string s = "100101";  
    bitset<4> bitset2(s);  //s的size=6,而bitset的size=4,只取前面部分,即1001

    char s2[] = "11101";
    bitset<4> bitset3(s2);  //与bitset2同理,只取前面部分,即1110

    cout << bitset1 << endl;  //00
    cout << bitset2 << endl;  //1001
    cout << bitset3 << endl;  //1110
  • 1
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

歸曦

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值