STL——标准模板库(一)

STL是标准模板库,主要分为:容器、算法、迭代器(重点)和适配器、分配器、仿函数。(了解)

容器 :系统为我们封装好的数据结构:数组(array、vector)、链表、栈、队列、树、hash表等

算法:系统为我们写好的算法:排序、交换、替换。

迭代器:链接容器和算法。

 

  一、Verctor

1、Verctor 是向量,变长数组。[

2、 需要头文件 #include <verctor>

3 访问方式:

Verctor <int> v;

pair <int,int > p;

v.push  //向数组添加元素

//   1使用下标访问

for(int i = 0;i<size();i++){

printf("%d",V[i]);

}  

//   2 使用迭代器访问  itartor

vector<int > :: iterator it;

for(vertor (int) ::iterator it = v.begin(); it!=end();it++){

printf("%d",V[i]);

 4、、常用函数

函数名 
函数作用 
push_back() 
在容器尾部添加元素
pop_back() 
在容器尾部删除元素
size() 
获取数组容量
clear() 
清空数组
insert(it,x)
在迭代器it位置添加x元素 
erase(it)/ erase(first,last)
删除迭代器it位置的元素 删除
[first,last) 位置的元素 

 

二、 queue&&prioity_queue 队列

quese

1、使用方式

#include<queue>
using namespace std;
queue<typename> name;

2、常用函数

函数名 函数功能
push()
将元素进队列 
front()/top()
访问队首元素 
back() 
访问队尾元素 
size()
获取队列大小 
pop()
将队首出队列 
empty() 
判断队列是否为空

 

Priority_queue   //优先队列 、堆

1、使用方式

#include<queue>
using namespace std;
priority_queue<typename> q;

2、访问方式

//第二个参数是承载堆的容器
priority_queue<int,vector<int>,greater<> > q; //大根堆 priority_queue<int,vector<int>,less<>> q;//小根堆
less 表示数字越大优先级越大,可替换参数:greater 表示数字越小优先级越大

3、常用函数

和queue一样,不过不能用pop()访问

 

三、stack

1、使用方式

#include<stack>
using namespace std;
stack<typename> name;

2、常用函数

 

 四、 map&&multimap  //映射

1、 使用方式

#include<map>
using namespace std;
map<typename,typename> name;

2、访问方式

复制代码
#include <cstdio>
#include <map>
using namespace std;
int main(int argc, char const *argv[])
{
map<char,int> mp;
map <string ,int >mp;
map<int,int>mp;
mp.clear();
mp['c'] = 12;
printf("%d\n",mp['c']);// 12
mp['d'] = 13;
mp['a'] = 1;
mp['f'] = 43;
mp['g'] = 1;
//map中key唯一,后添加的会覆盖前面的值
mp['c'] = 121;
printf("%d\n",mp['c']); // 121
for (map<char,int>::iterator it = mp.begin();
it!=mp.end(); it++)
 {
//通过it->first访问key的值,通过it->second访问value的值
printf("%c: %d\t",it->first,it->second);
}
return 0;
}
while(~scanf("%d",&a))
while(cin >> a)
复制代码

3、常用函数

 

 

 五、set //集合

  自动排序

1、使用方式

#include<set>
using namespace std;
set<typename> name;

2、 访问方式  只支持迭代器访问#include<stdio.h>#include<set>using namespace std;

复制代码
int main(){
set<int> s; s.insert(12); //添加元素
 ...
for(set<int>:: iterator it = s.begin();it!=s.end();it++){
printf("%d",*it);
 }
for(auto p : s) }
第一行一个数字t代表测试数据数量
第二行一个数字n代表数据个数
第三行n个数代表数据
3
3
1
2 3

4
3
2 1 5

5
1
2 3 4 5 #include<set> #include<iostream> using namespace std; const int N = 1000; int a[N]; set<int>s; int main(){ int t; cin >> t; while(t--){ s.clear(); int n; cin >> n; for(int i = 1; i <= n; i ++) { cin >> a[i];
s.insert(a[i]);

}
for(set<int>:: iterator it = s.begin();it!=s.end();it++){
printf("%d",*it);
 }
 }
return 0; }
复制代码

3、常用函数

 

 

六、 algorithm库中的算法/函数 

 

 

 sort  //排序函数

 

posted @   信2005-2赵磊  阅读(74)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构
· 字符编码:从基础到乱码解决
· 提示词工程——AI应用必不可少的技术
点击右上角即可分享
微信分享提示