c++容器
[TOC]
queue 队列
操作1:创建一个队列
queue<要存储的数据类型>队列的名字;
queue<int> q;操作2:获取长度
q.size(); 返回元素个数
int len = s.size(); // 获取长度,并且存储为有符号的整数
无符号数字不能直接和有符号数字比较操作3:获取队首元素
int l = q.front(); 读取第一个元素的值操作4:获取队尾元素
int r = q.back();返回q的末尾元素操作5:判断是否为空
q.empty(); 返回是否为空,空则返回1,否则返回0操作6:删除队首元素
q.pop();删除q的第一个元素操作7:队尾入队
q.push(1); 在q的队尾插入一个元素1完整使用
#include <bits/stdc++.h>
using namespace std;
/*
D1276 【例2-1】周末舞会
方法一、 创建两个队列 ,模拟排队
*/
queue<int> op1; // 男生队列
queue<int> op2; // 女生队列
int main()
{
for (int i = 1; i <= 3; ++i)
{
op1.push(i); // 将男生进行入队操作
}
for (int i = 1; i <= 5; ++i)
{
op2.push(i); // 将女生进行入队操作
}
int t;
t = 10;
while (t--)
{
cout << op1.front() << ' ' << op2.front() << endl; // 输出男生和女生
int x = op1.front();
int y = op2.front();
op1.pop();
op2.pop(); // 删除男生和女生的队头元素
op1.push(x);
op2.push(y);
}
return 0;
}stack 栈
操作1:创建一个栈
stack<数据类型>栈的名字;
比如stack<int> S;操作2:入栈O(1)
S.push(1); // 将元素1入栈操作3:出栈O(1)
S.pop(); // 删除栈顶元素 // 请注意如果此时的栈为空,获取栈顶元素会RE
if (S.size() != 0 )
{
S.pop();
}操作4:获取栈顶元素O(1)
int x = S.top(); // 请注意如果此时的栈为空,获取栈顶元素会RE
if (S.size() != 0 && S.top() == x)
{
}操作5:判断栈是否为空O(1)
S.empty(); // 为空的话返回1,否则返回0操作6:获取长度O(1)
int len = S.size(); // 获取长度完整使用
#include <bits/stdc++.h>
using namespace std;
stack<int> S;
int main()
{
S.push(1);
S.push(2);
S.push(3);
// 输出当前的栈顶元素和当前栈的大小
cout << S.top() << ' ' << S.size() << endl;
S.pop();
cout << S.top() << ' ' << S.size() << endl;
S.pop();
cout << S.top() << ' ' << S.size() << endl;
cout << "current status:" << S.empty() << endl;
S.pop();
// 输出当前的栈是否为空
cout << "current status:" << S.empty() << endl;
return 0;
}
vector 动态数组
操作1:创建一个动态数组
vector<存储的数据类型>名字;操作2:插入元素( 插入O(常数))
所有动态变化的容器,都需要注意,没有内存不能直接使用下标
vector<int>op;
op[1] = 1; 错误写法
op.push_back(1);// 只能用push_back将元素1放在最后面操作3:访问
可以使用下标访问,起点下标只能从0开始
cout << op[2] << endl;操作4:获取长度
op.size() // 有符号的操作5、清空向量
op.clear();操作6、插入元素( 插入O(常数))
op.insert(op.begin()+i,a);
i就是下标
将a元素插入到i+1号位置前面
相当于直接顶替 op[i]操作7:删除元素
op.erase(op.begin() + i); // 下标为i的元素,删除第i+1个元素操作8:初始化内存长度
op.resize(n);// 改变长度为n,默认初始值是0
op.resize(n,x);// 改变长度为n,全部修改为x
op.resize(n + 5);// 多加一点点操作9:排序
sort(op.begin(), op.end()); // 从小到大排序完整使用
#include <bits/stdc++.h>
using namespace std;
int main()
{
vector<int> vec;
vec.push_back(10);
vec.push_back(20);
vec.push_back(30);
int len = vec.size();
for (int i = 0; i < len; i++)
{
cout << vec[i] << ' ';
}
// 排序并输出
sort(vec.begin(), vec.end());
for (int i = 0; i < len; i++)
{
cout << vec[i] << ' ';
}
return 0;
}二分
首先二分查找有着两个常用的STL,分别是lower_bound和upper_bound;
默认条件:数组本身是从小到大排序的
lower_bound:返回大于等于x的第一个位置
参数:lower_bound(起点地址,终点地址+1,查找值),返回数据是地址。
查找a数组在区间范围[L,R]之间大于等于x的第一个下标:lower_bound(a + L, a + R + 1, x) - a; // 减去的作用是删去起点地址
如果数组之中没有找到大于等于x的位置,返回最大下标+1的位置。
完整使用
#include <bits/stdc++.h>
using namespace std;
int a[10] = {0, 1, 2, 3, 4};
int main()
{
int pos = lower_bound(a + 1, a + 4 + 1, 4) - a;
cout << "查找大于等于数字4的第一个下标" << pos << endl;
pos = lower_bound(a + 1, a + 4 + 1, 5) - a;
cout << "查找大于等于数字5的第一个下标" << pos << endl;
// 此处的5是因为没有找到,所以返回5,也就是最大下标 + 1
return 0;
}
upper_bound:返回大于x的第一个位置
参数:upper_bound(起点地址,终点地址+1,查找值),返回数据是地址。
查找a数组在区间范围[L,R]之间大于x的第一个下标:upper_bound(a + L, a + R + 1, x) - a; // 减去的作用是删去起点地址
如果数组之中没有找到大于x的位置,返回最大下标+1的位置。
#include <bits/stdc++.h>
using namespace std;
int a[10] = {0, 1, 2, 3, 4};
int main()
{
int pos = upper_bound(a + 1, a + 4 + 1, 3) - a;
cout << "查找大于3的第一个下标" << pos << endl;
pos = upper_bound(a + 1, a + 4 + 1, 4) - a;
cout << "查找大于数字4的第一个下标" << pos << endl;
// 此处的5是因为没有找到大于4的数字,所以返回5,也就是最大下标 + 1
return 0;
}应用范围
注明:所有的操作都是针对数组本身数据从小到大排序。
1、查找是否有x的存在。
int pos = lower_bound(a + 1, a + n + 1, x) - a;
然后判断a[pos]是否等于x。
2、查找大于等于x的位置或者查找大于x的位置,即两个STL的基本定义。
3、查找小于x的最后一个位置。
int pos = lower_bound(a + 1, a + n + 1, x) - a;// 查找大于等于x的第一个位置
判断pos - 1是否越界,如果没有,pos - 1就是小于x的第一个位置。
4、查找小于等于x的最后一个位置。
int pos = upper_bound(a + 1, a + n + 1, x) - a;// 查找大于x的第一个位置
判断pos - 1是否越界,如果没有,pos - 1就是小于等于x的最后一个位置。
5、查找数组之中等于x的数字个数。
int r = upper_bound(a + 1, a + n + 1, x) - a;// 查找大于x的第一个位置
r = max(1, r - 1); // 小于等于x的最后一个位置(但是不能越界)
int l = lower_bound(a + 1, a + n + 1, x) - a;// 查找大于等于x的第一个位置
此时的l到r之间的长度就代表相等的数字个数(从第一个大于等于的位置,到最后一个相等的位置)
答案就是r - l + 1。priority_queue 优先队列
特点:放在内部的元素出队的时候存在优先级
大根堆:出队的时候返回最大值。
小根堆:出队的时候返回最小值。
假设结构体是Point类型
struct Point{
int x, y;
};
两种赋值方式
① Point A;
A.x = ......
② Point A = Point{1, 2};
整体赋值,将括号内的东西按照先后顺序赋值给x和y 重点:类型必须一致,比如y是double类型,赋值的时候必须写{0.0},不然系统编译会认为数据类型对不上。:
操作1:创建一个优先队列
priority_queue<数据类型,存储容器,排序规则> 优先队列名字
每次可以从队列之中拿到最大的数值,默认大根堆:priority_queue<存储的数据类型> 优先队列名字;
默认写法可以直接只写一个数据类型。操作2:插入元素
Q.push(x); //将x放入队列之中,会自动将最大值放在队首 O(logn)操作3:删除元素
Q.pop(); 删除操作,删除队首元素操作4:获取队首元素
Q.top(); 获取队首元素,但是不删除操作5:获取队列长度
Q.size(); 获取队列长度操作6:返回是否空
Q.empty(); 获取队列是否为空,是空返回1,否则返回0重写比较函数
小于号重写:优先队列之中
bool operator < (const 结构体类型 &A)const
{
左边的不需要写名字,但是右边的现在就是A
return t > A.t; // t代表左边的t,A.t代表右边的t
// 从大到小排序
}完整使用
#include <bits/stdc++.h>
using namespace std;
struct Node
{
int value;
bool operator<(const Node &other) const
{
return value < other.value; // 定义优先级,这里是大顶堆
}
};
int main()
{
priority_queue<Node> pq;
pq.push(Node{5});
pq.push(Node{1});
pq.push(Node{10});
cout << pq.top().value << endl; // 输出最高优先级的元素的值,即 10
pq.pop();
cout << pq.top().value << endl; // 输出新的最高优先级的元素的值,即 5
return 0;
}set 集合
特点:放在内部的元素会自动排序+去重
操作1:创建一个集合
set<int> s;操作2、获取长度
时间复杂度:O(1)
s.size();操作3:检查是否为空
时间复杂度:O(1)
s.empty()
如果为空:返回true
不为空:返回false操作4:清空
s.clear();操作5:统计个数
s.count(x)
因为本身元素去重,所以只能返回0或者1
// 是否存在x这个数字操作6:lower_bound和upper_bound
auto it = s.lower_bound(2); //会自动返回大于等于2的位置的指针
// 但是一般会出现it做减法的情况
// 所以为了防止越界,提前插入一个极小值和一个极大值
auto it = s.upper_bound(2); //会自动返回大于2的位置的指针
cout << *it; // 返回值
it--; // 找左边的指针
操作7:erase删除
// 使用值来删除元素
s.erase(2); // 删除元素2
// 使用迭代器来删除元素
auto it = s.begin();
s.erase(it); // 删除第一个元素操作8:插入
s.insert(1);完整使用
访问所有元素:
双向迭代器
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int main()
{
for (int i = 1; i <= 10; i++)
{
s.insert(i);
}
set<int>::iterator it;
for(it = s.begin(); it != s.end(); it++)
{
cout << *it << ' ';
}
return 0;
}
使用auto关键词
#include <bits/stdc++.h>
using namespace std;
set<int> s;
int main()
{
for (int i = 1; i <= 10; i++)
{
s.insert(i);
}
for (auto it : s)
{
cout << it << ' ';
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
set<int> s;
// 向 set 中插入元素
s.insert(1);
s.insert(4);
s.insert(2);
s.insert(5);
s.insert(6);
// 查找不小于 2 的元素
auto it = s.lower_bound(2);
cout << "The lower bound of key 2 is " << (*it) << endl;
// 查找不小于 3 的元素
it = s.lower_bound(3);
cout << "The lower bound of key 3 is " << (*it) << endl;
// 查找不小于 8 的元素
it = s.lower_bound(8);
if (it != s.end())
{
cout << "The lower bound of key 8 is " << (*it) << endl;
}
else
{
cout << "The lower bound of key 8 is past the end of the set" << endl;
}
return 0;
}string 字符串
操作1:创建一个字符串
string s;操作2:截取字符串
s.substr(起点下标, 长度);
s.substr(1, 2);操作3:插入字符串
insert(pos, str); //在指定位置pos插入字符串str操作4:删除字符串
erase(pos, count); //从指定位置pos开始删除count个字符。
erase(pos); //删除指定位置pos的字符(pos是一个迭代器)。
比如erase(s.begin() + pos);map 超级桶
操作1:创建一个超级桶
map<数据类型1,数据类型2>map的名字
map<string,int>op;操作2:赋值
op["ABC"] = 1;
op[数据类型1] = 数据类型2访问所有元素:
双向迭代器
map<string,double>::iterator it;
for (it=op.begin();it!=op.end();++it)
{
it->first;// 数据类型1
it->second;//数据类型2
}使用auto关键词
for (auto it : op)
{
it.first;// 数据类型1
it.second; // 数据类型2
}完整使用
#include <bits/stdc++.h>
using namespace std;
/*
原文 密文
你 1
我 2
好 3
AK 4
IOI 5
! 6
加密之后的13, 2456
map<数据类型1, 数据类型2>名称
op[数据类型1] = 数据类型2
*/
map<int, string> op;
int num[] = {1, 3, 7, 2, 4, 5, 6};
int main()
{
op[1] = "你";
op[2] = "我";
op[3] = "好";
op[4] = "AK";
op[5] = "IOI";
op[6] = "!";
op[7] = ",";
for (int i = 0; i < 7; i++)
{
cout << op[num[i]] << ' ';
}
cout << endl;
for (auto it:op) // auto自动获取类型 将op之中的元素一个一个弹出到it之中
{
cout << it.first << ' '; // 数据类型1
/*
map会自动把数据类型1按照字典序从小到大排序
弹出的内容不一定是原来的顺序
*/
cout << it.second << endl; // 数据类型2
}
return 0;
} 






